954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

2player tic tac toe

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.

anallan
Newbie Poster
9 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

anallan
Newbie Poster
9 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

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?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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..

anallan
Newbie Poster
9 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Post a program which crashes, then we can specifically explain what is going wrong.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

gaowei
Newbie Poster
18 posts since Jun 2007
Reputation Points: 26
Solved Threads: 1
 

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
#include

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
*/

anallan
Newbie Poster
9 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Read this - http://www.daniweb.com/forums/announcement118-3.html
Didn't you see the watermark behind the edit window telling you to use code tags?
Did you use 'preview post' before pressing submit, to make sure your presentation was as good as it could be?

With any luck, you'll be able to edit your own post to fix it. Otherwise wait for a mod.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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");
	}
}
alsoumhi
Junior Poster in Training
66 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

please reply to me if it is correct or not

alsoumhi
Junior Poster in Training
66 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

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

anallan
Newbie Poster
9 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 
please reply to me if it is correct or not

Obviously not, sinceNarue 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...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You