my professor gave this machine problem as a part of our finals.can you help me out/
here are the instructions:

2 player tictactoe

-program a 2player tictactoe game wherein two players alternately key-in the coordinates to which they want to place a 'o' or an 'x'
-the game board is a 3x3 array
-the first player marks the array with 'o's;the second marks the array with 'x's
-the game starts with an array filled with dashes;players take turns in marking the array;a marked cell can no longer be marked
-declare that a player won if he has marked three cells in a row,column or diagonal;if the array is filled-up without a winner,declare that nobody won..


Sample Run:

_______________________________________________________


Welcome to the tic-tac-toe game

Player 1, input coordinates: 1 1
o--
---
---


Player 2, input coordinates: 2 2
o--
-x-
---

Player 1, input coordinates: 2 1
o--
ox-
---


Player 2, input coordinates: 1 3
o-x
ox-
---

Player 1, input coordinates: 3 1
o-x
ox-
o--

Player 1 wins!

___________________________________________________

i hope someone can help me on this.
thanks.

Recommended Answers

All 13 Replies

OK, so what is your actual question?

You've laid out the requirements quite nicely, but we're not here to do your homework for you.
http://www.daniweb.com/forums/announcement118-2.html

To get our attention, you need to offer up some kind of attempt and ask a specific question.

Besides, if these are your finals, then I have to say you really ought to be able to make a start yourself.

well,i didnt actually asked for the program..sorry if i seemed to imply that..
you see,im having troubles on pointers and such(im still a newbie when it comes to c-programming)..well,i thought i can get some help on how i could organize the program with the use of pointers(which,according to my prof,is a must-see in order to pass)..
T_T

Again, no code, so it's hard to see what the question is, apart from the fact that you're having problems.

I'm going to take a stab and say that you're having problems passing your 2D array to a function.

Like you have
char game[3][3];

And you have a function like
void drawBoard( char **game );

and it just isn't working for you.

Am I close?

umm..that's one of the problems i have. i wanna ask,what's up when it says something like core error or something.at first my program is running then it crashes.waahh..
next,i have this problem on pointers,how can you get pointers to "crawl" from one array to another in a continuous motion?im making use of loops but i just end up overwriting this and that.Example of which was when i had this assignment to make a program that will arrange ten integers in an array in a decreasing order.Using pointers,i end up having them only shuffled..Im so dumb with these pointers..tsk..

First,you must alone think this question,you must believe yourself.

here's the codes of my flawed program..still there are errors..can you help me out??ive got sntax errors..i think somehow i got the idea how to do things but,syntactically,im lacking...T_T

#include <stdio.h>
#include <stdlib.h>

char createTable(char x[3][3])  //function to fill the array with dashes and print the inital table.
{
    int i=0, j=0;
        while (i<3)
    {
        while (j<3)
        {
            x[i][j]='-';
            printf("Welcome to Tic-Tac-Toe Extreme!\n");
            printf("%c\n", x[i][j]);
        }
    }
    return x[][];
}

void printTable(char x[3][3])  //function to print the new board after a player's turn
{
    int i=0, j=0;
    while (i<3)
    {
        while (j<3)
        {
            printf("%c\n", x[i][j]);
        }
    }
}

char player1(char x[3][3])  //function for player 1's turn
{
    int i, j;
    char position;  

    printf("Player 1, input position:\n");
    scanf("%c\n", &position);   
    switch(position)
    {   case 'A':
        case 'a': i=1, j=1; break;
        case 'B':
        case 'b': i=1, j=2; break;
        case 'C':
        case 'c': i=1, j=3; break;
        case 'D':
        case 'd': i=2, j=1; break;
        case 'E':
        case 'e': i=2, j=2; break;
        case 'F':
        case 'f':   i=2, j=3; break;
        case 'G':
        case 'g': i=3, j=1; break; 
        case 'H':
        case 'h': i=3, j=2; break;
        case 'I': 
        case 'i': i=3, j=3; break;
        default : printf("error\n");
                  x[][] = player1(x[][]);
                  break;
    }

    if(x[i][j]=='-')    
    {
        x[i][j]='O';
        printTable(x[3][3]);
    }
    else printf("Please choose another cell.\n");
    return x[][];   
}

char player2(char x[3][3])  //function for player 2's turn
{
    int i, j;
    char position;  

    printf("Player 2, input position:\n");
    scanf("%c",&position); 
    switch(position)
    {   case 'A':
        case 'a': i=1, j=1; break;
        case 'B':
        case 'b': i=1, j=2; break;
        case 'C':
        case 'c': i=1, j=3; break;
        case 'D':
        case 'd': i=2, j=1; break;
        case 'E':
        case 'e': i=2, j=2; break;
        case 'F':
        case 'f':   i=2, j=3; break;
        case 'G':
        case 'g': i=3, j=1; break; 
        case 'H':
        case 'h': i=3, j=2; break;
        case 'I': 
        case 'i': i=3, j=3; break;
        default : printf("error");
                  x[][] = player2(x[][]);
                  break;
    }

    if(x[i][j]=='-')    //checks if there's still a cell to occupy
    {
        x[i][j]='X';
        printTable(x[][]);
    }
    else printf("Please choose another cell.\n");
    return x[][];
}

int checkWinner(char x[3][3], char symbol)
{
    int win=0;      //boolean is initially 0 or false

    if(x[1][1]==symbol && x[1][2]==symbol && x[1][3]==symbol || /*check ABC*/
       x[2][1]==symbol && x[2][2]==symbol && x[2][3]==symbol || /*check DEF*/
       x[3][1]==symbol && x[3][2]==symbol && x[3][3]==symbol || /*check GHI*/
       x[1][1]==symbol && x[2][1]==symbol && x[3][1]==symbol || /*check ADG*/
       x[1][2]==symbol && x[2][2]==symbol && x[3][2]==symbol || /*check BEH*/
       x[1][3]==symbol && x[2][3]==symbol && x[3][3]==symbol || /*check CFI*/
       x[1][1]==symbol && x[2][2]==symbol && x[3][3]==symbol || /*check AEI*/
       x[1][3]==symbol && x[2][2]==symbol && x[3][1]==symbol)   /*check CEG*/
    win=1;      //boolean will become 1 or true if there is a winner

    else win=0; 

    return win;

}

main()
{
    char board[3][3];   //create variable for the board, an array with 3 rows and 2 columns
    int countTurn=0;    //counts the number of turns so far
    int hasWinner=0;    //tells if there is already a winner, 0 for no winner and 1 for has winner
    char symbol;    

    board[][]=createTable(board[][]);
    while(hasWinner!=1)
    {
        board[][]=player1(board[][]);
        countTurn++;
        if(countTurn>=4)    //checking if there is a winner after every turn starting on the fourth turn 
        {   
            hasWinner=checkWinner(board[][], 'O');  
        }
        else
        {
            board[][]=player2(board[][]);
            countTurn++;
            if(countTurn>=4)
            {   
                hasWinner=checkWinner(board[][], 'X');
            }   
        }
    }   
}

///////////*notes*/////////////
/*
    a=1 1   b=1 2   c=1 3
    d=2 1   e=2 2   f=2 3
    g=3 1   h=3 2   i=3 3
    possible positions of wins: abc, def, ghi, adg, beh, cfi, aei, ceg
*/

I think this what you want

#include<stdio.h>
#include<stdlib.h>
char check(void);
char array[3][3]={'-','-','-','-','-','-','-','-','-'};
void display();
int main()
{
  int i=0,l=0,x,y;
  int  k=1;
 char done;

for(i=0;i<9;i++)
  {
     if(k==1)
      {
printf("player %d,Input your coordinate:",k);
scanf("%d %d",&x,&y);
  array[x][y]='x';
  display();
if(check()  != '-')
break;
k++;
  }
 else
  {
printf("player %d,Input your coordinate:",k);
 scanf("%d %d",&x,&y);
  array[x][y]='o';
  display();
 if(check()  != '-')
 break;
 --k;
 }
  }
   done=check();

   if(done=='x')
   printf("\nthe winner is player 1\n");

   else if(done=='o')
   printf("\nthe winner is player 2\n");

   else
   printf("No winners");

   system("PAUSE");

return 0;

}

char check(void)
{
   int i;
 char letter='-';
  for(i=0; i<3; i++)  /* check rows */
if(array[i][0]==array[i][1] && array[i][0]==array[i][2]) 
letter= array[i][0];

for(i=0; i<3; i++)  /* check columns */
 if(array[0][i]==array[1][i] && array[0][i]==array[2][i] ) 
letter= array[0][i];
  /* test diagonals */
 if( array[0][0]==array[1][1] && array[1][1]==array[2][2] )
letter= array[0][0];
  if( array[0][2]==array[1][1] && array[1][1]==array[2][0] )
letter= array[0][2];

return letter;

}
void display()
{
    int i,l;
    
    for(i=0;i<=2;i++)
	{
		for(l=0;l<=2;l++)
		{
			printf("%c\t",array[i][l]);
		}
       
		printf("\n");
	}
}

please reply to me if it is correct or not

sorry, i wasnt able to give attention to the watermark..T_T

please reply to me if it is correct or not

Obviously not, since Narue added the code tags for you. And you did not explain what your problem is. When someone suggests you read something, you should do it. Go back and read Salem's link -- and also read this

sorry, i wasnt able to give attention to the watermark..T_T

Why not? It was put there for you...

#include<iostream>
using namespace std;
char a[3][3]={};// A 3*3 character matrix to contain X's and O's
/* This function returns true if a winner is decided otherwise it returns false */
bool Winner(char a[3][3])
{
     if((a[0][0]=='X'||a[0][0]=='O') && (a[0][0]==a[0][1]) && (a[0][1]==a[0][2]))return true;
     else if((a[0][0]=='X'||a[0][0]=='O') && (a[0][0]==a[1][0]) && (a[0][0]==a[2][0]))return true;
     else if((a[1][0]=='X'||a[1][0]=='O') && (a[1][0]==a[1][1]) && (a[1][1]==a[1][2]))return true;
     else if((a[2][0]=='X'||a[2][0]=='O') && (a[2][0]==a[2][1]) && (a[2][1]==a[2][2]))return true;
     else if((a[0][1]=='X'||a[0][1]=='O') && (a[0][1]==a[1][1]) && (a[1][1]==a[2][1]))return true;
     else if((a[0][2]=='X'||a[0][2]=='O') && (a[0][2]==a[1][2]) && (a[1][2]==a[2][2]))return true;
     else if((a[0][0]=='X'||a[0][0]=='O') && (a[0][0]==a[1][1]) && (a[1][1]==a[2][2]))return true;
     else if((a[0][2]=='X'||a[0][2]=='O') && (a[0][2]==a[1][1]) && (a[1][1]==a[2][0]))return true;
     else return false;
}
/* This function returns true if the game is over and all the matrix elements are filled */
bool Full(char a[3][3])
{
     if(a[0][0] && a[0][1] && a[0][2] && a[1][0] && a[1][1] && a[1][2] && a[2][0] && a[2][1] && a[2][2])return true;
     else return false;
}
int main()
{
    int n;
    cout<<"\n\nWelcome to the classic game of Tic Tac Toe.For now sake,there will be 2 players playing on a 3*3 grid  with each block having numbers as follows :-\n\n\n";
    cout<<"_1_|_2_|_3_\n_4_|_5_|_6_\n 7 | 8 | 9\n\n";
    cout<<"Player 1,shoot your move (a number that is not already chosen) buddy with a block number.your alphabet is X\n\n";
    cin>>n;cout<<"\n\n";if(n%3)a[n/3][(n+2)%3]='X';else a[n/3-1][(n+2)%3]='X';
    cout<<"_1_|_2_|_3_                                _"<<a[0][0]<<"_|_"<<a[0][1]<<"_|_"<<a[0][2]<<"_\n";
    cout<<"_4_|_5_|_6_                                _"<<a[1][0]<<"_|_"<<a[1][1]<<"_|_"<<a[1][2]<<"_\n";
    cout<<" 7 | 8 | 9                                  "<<a[2][0]<<" | "<<a[2][1]<<" | "<<a[2][2]<<" \n\n\n";
    while(!Winner(a) && !Full(a))
    {
                     cout<<"Player 2,shoot your move buddy (a number that is not already chosen) with a block number.your alphabet is O\n\n";
                     cin>>n;cout<<"\n\n";if(n%3)a[n/3][(n+2)%3]='O';else a[n/3-1][(n+2)%3]='O';
                     cout<<"_1_|_2_|_3_                                _"<<a[0][0]<<"_|_"<<a[0][1]<<"_|_"<<a[0][2]<<"_\n";
                     cout<<"_4_|_5_|_6_                                _"<<a[1][0]<<"_|_"<<a[1][1]<<"_|_"<<a[1][2]<<"_\n";
                     cout<<" 7 | 8 | 9                                  "<<a[2][0]<<" | "<<a[2][1]<<" | "<<a[2][2]<<" \n\n\n";
                     if(Winner(a)){cout<<"\n\n                                   PLAYER 2 WINS.CONGRATULATIONS :-) \n\n\n";break;}
                     cout<<"Player 1,shoot your move buddy (a number that is not already chosen) with a block number.your alphabet is X\n\n";
                     cin>>n;cout<<"\n\n";if(n%3)a[n/3][(n+2)%3]='X';else a[n/3-1][(n+2)%3]='X';
                     cout<<"_1_|_2_|_3_                                _"<<a[0][0]<<"_|_"<<a[0][1]<<"_|_"<<a[0][2]<<"_\n";
                     cout<<"_4_|_5_|_6_                                _"<<a[1][0]<<"_|_"<<a[1][1]<<"_|_"<<a[1][2]<<"_\n";
                     cout<<" 7 | 8 | 9                                  "<<a[2][0]<<" | "<<a[2][1]<<" | "<<a[2][2]<<" \n\n\n";
                     if(Winner(a)){cout<<"\n\n                                   PLAYER 1 WINS.CONGRATULATIONS :-) \n\n\n";break;}
    }
    if(Full(a) && !Winner(a))cout<<"\n\n                                         Nobody wins guys... It was a close match,though\n\n";
    system("pause");
    return 0;
}

I did this for my younger sister recently for a middle school project.this might help :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.