13.12.09

Utk Dadar

Maaf y menganggu, haha. ni gw post di sini bwt temen gw ndaru, dy mw liat, tapi gw gx bisa kirim lewat email. Speedy gw 3 menit mati, gx sempet. Gembel emank. Nah gw kn nulis blog pake Windows Live Writer (offline) jadi cuman butuh online buat upload postnya aj.
Ok d here it goes..

Tic Tac Toe gembel nan sederhana di Visual Studio C buatan gw:

#include <stdio.h>
#include <windows.h>
void main()
{
    char reset;
    do {
    //deklarasi variabel
    int map [] [7]= {
        {1,1,1,1,1,1,1},
        {1,0,1,0,1,0,1},
        {1,1,1,1,1,1,1},
        {1,0,1,0,1,0,1},
        {1,1,1,1,1,1,1},
        {1,0,1,0,1,0,1},
        {1,1,1,1,1,1,1},
        };
    int a,b,x=1,y=1,p=1,win=0,s;

    //Judul
    printf ("\n       Tic Tac Toe       ");
    printf ("\n  ---------------------  ");
    printf("\n   By: Sherly Septiani   \n\n\n");

    //map awal
    for (a=0 ; a<7 ; a++)
    {   printf ("         ");
        for (b=0; b<7; b++)
        {
            if (map [a] [b]==1) printf ("#");
            else if (map [a] [b]==0) printf (" ");
        }
        printf ("\n");
    }

    do {
    //input player
        do {
            printf ("\n[P%d] Masukkan koordinat x: ",p);
            scanf ("%d",&y); fflush (stdin);
            printf ("[P%d] Masukkan koordinat y: ",p);
            scanf ("%d",&x); fflush (stdin);
            if (x>3 ||x<1 || y>3 ||y<1 || (map [2*x-1][2*y-1] !=0))  //skip pengisian array
                {printf ("-==P%d, input Anda salah!!==-", p); };
                printf ("\n");   
        }
        //validasi x & y
        while ((x>3 ||x<1) || (y>3 ||y<1) || (map [2*x-1][2*y-1] !=0));
        if (p==1) map [2*x-1][2*y-1]=2;
        else if (p==2) map [2*x-1][2*y-1]=3;

    //print map
    printf ("\n");
    for (a=0 ; a<7 ; a++)
    {   printf ("         ");
        for (b=0; b<7; b++)
        {
            if (map [a] [b]==1) printf ("#");
            else if (map [a] [b]==0) printf (" ");
            else if (map [a] [b]==2) printf ("%c",3);
            else if (map [a] [b]==3) printf ("x");
        }
        printf ("\n");
    }

    //win validation
        if (map [1][1]==(p+1) && map [1][3]==(p+1) && map [1][5]==(p+1)) win=1;
        else if (map [3][1]==(p+1) && map [3][3]==(p+1) && map [3][5]==(p+1)) win=1;
        else if (map [5][1]==(p+1) && map [5][3]==(p+1) && map [5][5]==(p+1)) win=1;
        else if (map [1][1]==(p+1) && map [3][1]==(p+1) && map [5][1]==(p+1)) win=1;
        else if (map [1][3]==(p+1) && map [3][3]==(p+1) && map [5][3]==(p+1)) win=1;
        else if (map [1][5]==(p+1) && map [3][5]==(p+1) && map [5][5]==(p+1)) win=1;
        else if (map [1][1]==(p+1) && map [3][3]==(p+1) && map [5][5]==(p+1)) win=1;
        else if (map [1][5]==(p+1) && map [3][3]==(p+1) && map [5][1]==(p+1)) win=1;

        //seri (tidak ad yang menang)
        else if
            (map[1][1]!=0 &&
            map[5][1]!=0 &&
            map[1][3]!=0 &&
            map[5][3]!=0 &&
            map[1][5]!=0 &&
            map[5][5]!=0 &&
            map[3][1]!=0 &&
            map[3][3]!=0 &&
            map[3][5]!=0) win=2;

        if (p==1) p=2;
       else if (p==2) p=1; 
    }
    while (win==0);

    //win
    if (win==1) {
    if (p==1) p=2;
    else if (p==2) p=1;
    printf ("\n");
    for(int i=0;i<3;i++)
    {
        printf(">");
        Sleep(300);
    }
    printf (" Player %d, Anda menang! ", p);
    for(int s=0;s<3;s++)
    {
        printf("<");
        Sleep(300);
    }
    };

    //seri
    if (win==2) printf ("\n**Seri [tidak ada yang menang]**");

    printf ("\n");
    //game reset
    do {
        printf ("\nIngin mengulang [y/n]? ");
        scanf ("%c", &reset); fflush (stdin);
        }
    while (toupper (reset)!= 'Y' && toupper (reset)!= 'N');
    }
    while (toupper (reset)=='Y');
    if (toupper (reset)=='N') printf ("\nTerimakasih telah memainkan game ini.. :)");

    getchar ();
    }

1 komentar:

Anonim mengatakan...

nice bgt ne tic tac toenya ..
jadi dpt inspirasi dari codingannya
:D