2player tic tac toe

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2007
Posts: 9
Reputation: anallan is an unknown quantity at this point 
Solved Threads: 0
anallan anallan is offline Offline
Newbie Poster

2player tic tac toe

 
0
  #1
Sep 22nd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: 2player tic tac toe

 
0
  #2
Sep 22nd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 9
Reputation: anallan is an unknown quantity at this point 
Solved Threads: 0
anallan anallan is offline Offline
Newbie Poster

Re: 2player tic tac toe

 
0
  #3
Sep 24th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: 2player tic tac toe

 
0
  #4
Sep 24th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 9
Reputation: anallan is an unknown quantity at this point 
Solved Threads: 0
anallan anallan is offline Offline
Newbie Poster

Re: 2player tic tac toe

 
0
  #5
Sep 24th, 2007
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..
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: 2player tic tac toe

 
0
  #6
Sep 24th, 2007
Post a program which crashes, then we can specifically explain what is going wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 20
Reputation: gaowei is an unknown quantity at this point 
Solved Threads: 1
gaowei gaowei is offline Offline
Newbie Poster

Re: 2player tic tac toe

 
0
  #7
Sep 25th, 2007
First,you must alone think this question,you must believe yourself.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 9
Reputation: anallan is an unknown quantity at this point 
Solved Threads: 0
anallan anallan is offline Offline
Newbie Poster

here's the codes ive created..

 
0
  #8
Oct 11th, 2007
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
*/
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: 2player tic tac toe

 
0
  #9
Oct 11th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 63
Reputation: alsoumhi is an unknown quantity at this point 
Solved Threads: 0
alsoumhi alsoumhi is offline Offline
Junior Poster in Training

Re: 2player tic tac toe

 
0
  #10
Oct 11th, 2007
I think this what you want
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. char check(void);
  4. char array[3][3]={'-','-','-','-','-','-','-','-','-'};
  5. void display();
  6. int main()
  7. {
  8. int i=0,l=0,x,y;
  9. int k=1;
  10. char done;
  11.  
  12. for(i=0;i<9;i++)
  13. {
  14. if(k==1)
  15. {
  16. printf("player %d,Input your coordinate:",k);
  17. scanf("%d %d",&x,&y);
  18. array[x][y]='x';
  19. display();
  20. if(check() != '-')
  21. break;
  22. k++;
  23. }
  24. else
  25. {
  26. printf("player %d,Input your coordinate:",k);
  27. scanf("%d %d",&x,&y);
  28. array[x][y]='o';
  29. display();
  30. if(check() != '-')
  31. break;
  32. --k;
  33. }
  34. }
  35. done=check();
  36.  
  37. if(done=='x')
  38. printf("\nthe winner is player 1\n");
  39.  
  40. else if(done=='o')
  41. printf("\nthe winner is player 2\n");
  42.  
  43. else
  44. printf("No winners");
  45.  
  46. system("PAUSE");
  47.  
  48. return 0;
  49.  
  50. }
  51.  
  52. char check(void)
  53. {
  54. int i;
  55. char letter='-';
  56. for(i=0; i<3; i++) /* check rows */
  57. if(array[i][0]==array[i][1] && array[i][0]==array[i][2])
  58. letter= array[i][0];
  59.  
  60. for(i=0; i<3; i++) /* check columns */
  61. if(array[0][i]==array[1][i] && array[0][i]==array[2][i] )
  62. letter= array[0][i];
  63. /* test diagonals */
  64. if( array[0][0]==array[1][1] && array[1][1]==array[2][2] )
  65. letter= array[0][0];
  66. if( array[0][2]==array[1][1] && array[1][1]==array[2][0] )
  67. letter= array[0][2];
  68.  
  69. return letter;
  70.  
  71. }
  72. void display()
  73. {
  74. int i,l;
  75.  
  76. for(i=0;i<=2;i++)
  77. {
  78. for(l=0;l<=2;l++)
  79. {
  80. printf("%c\t",array[i][l]);
  81. }
  82.  
  83. printf("\n");
  84. }
  85. }
Last edited by Narue; Oct 11th, 2007 at 4:42 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2236 | Replies: 12
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC