| | |
2player tic tac toe
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2007
Posts: 9
Reputation:
Solved Threads: 0
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.
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.
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.
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.
•
•
Join Date: Sep 2007
Posts: 9
Reputation:
Solved Threads: 0
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
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?
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?
•
•
Join Date: Sep 2007
Posts: 9
Reputation:
Solved Threads: 0
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..
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..
•
•
Join Date: Sep 2007
Posts: 9
Reputation:
Solved Threads: 0
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
*/
#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
*/
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.
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.
•
•
Join Date: Aug 2007
Posts: 63
Reputation:
Solved Threads: 0
I think this what you want
C Syntax (Toggle Plain Text)
#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"); } }
Last edited by Narue; Oct 11th, 2007 at 4:42 pm. Reason: Added code tags
![]() |
Similar Threads
- Tic-Tac-Toe (C++)
- Tic Tac Toe Homework (C++)
- Tic Tac toe help (C)
- Tic Tac Toe AI help, where to reset variables. (C++)
Other Threads in the C Forum
- Previous Thread: C Can't Handle Pointers
- Next Thread: can someone help me with this C programm
Views: 2236 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm command copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






