| | |
Tic Tac Toe
![]() |
•
•
Join Date: Jul 2009
Posts: 1
Reputation:
Solved Threads: 0
I'm a beginner at c++ and I have to do a game of tic tac toe and I'm having problems with asking the player to play. Please help.
*****************************************************
*****************************************************
C++ Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; ///function prototype void inttableArray(int [][3], int ); void displayArray (int [][3], int ); void inputArrayFunction(int [][3], int ); void getMove(int [][3], int ); void getComputerMove(int [][3], int ); char Check(int [][3], int ); int main() { char end; end= ' '; int tableOne[3][3]; //array of 3 row and 5 columns cout<<"This is the game of Tic Tac Toe.\n"; cout<<"You will be playing against the computer.\n"; inttableArray(tableOne, 3); displayArray(tableOne, 3); getMove(tableOne, 3); end = Check(tableOne, 3); getComputerMove(tableOne, 3); inputArrayFunction(tableOne, 3); if(end== ' '); if(end=='X') cout<<"You won!"; else cout<<"I won!!!!"; displayArray(tableOne, 3); return 1; } //************************************************** void inttableArray(int TempArray[][3], int size ) { int row, column; for ( row = 0; row <3; row ++) for (column = 0; column < size; column ++) TempArray[row][column] = ' '; } //************************************************** void getMove(int TTempArray[][3], int Tsize) { int x =0; int y=0; int n=0; cout<<" Please make your move: "; cin>>n; if(n<1||n>9) cout<<"Invalid move"<<endl; else if(TTempArray[x][y]!= ' ') { cout<<"Invalid move, try again.\n"; getMove(TTempArray, 3); } else TTempArray[x][y] = 'X'; } //************************************************** void getComputerMove(int STempArray[][3], int Ssize) { int x; int y; int n; cout<<"Player 2"<<endl; cout<<" Please make your move: "; cin>>n; if(n<1||n>9) cout<<"Invalid move"<<endl; else if(n=' ';n<=9); if(STempArray[y][x]!= ' ') { cout<<"Invalid move, try again.\n"; getMove(STempArray, 3); } else STempArray[y][x] = 'Y'; } //************************************************** void displayArray (int DTempArray[][3], int Dsize) { int x=0; int y=0; for ( x = 0; x < 3; x ++) { printf(" %c | %c | %c ",DTempArray[x][0], DTempArray[x][1], DTempArray [x][2]); if(x!=2) printf("\n---|---|---\n"); } printf("\n"); } //************************************************** void inputArrayFunction(int inputArray[][3], int inputsize) { int a, b; int x = 1; printf("\n---|---|---\n"); for ( a = 0 ;a < 3; a++) for (b = 0; b < inputsize; b++) if ( b == 2 ) inputArray[a][b] = 10; else inputArray[a][b] = b; } //************************************************** char Check(int TinputArray[][3], int Tinputsize ) { int i; int row; int tableOne[3][3]; for ( row = 0; row <3; row ++) if(tableOne[row][0]==tableOne[row][1] && tableOne[row][0]==tableOne[row][2]) return tableOne[row][0]; for(i=0; i<3; i++) if(tableOne[0][row]==tableOne[1][row] && tableOne[0][row]==tableOne[2][row]) return tableOne[0][row]; if(tableOne[0][0]==tableOne[1][1] && tableOne[1][1]==tableOne[2][2]) return tableOne[0][0]; if(tableOne[0][2]==tableOne[1][1] && tableOne[1][1]==tableOne[2][0]) return tableOne[0][2]; return ' '; }
I didn't really look at anything accept for the main function. Wouldn't it make sense to have a loop? It looks like your only calling the human to move once, then the computer the move once. Then the game ends... Shouldn't you loop until someone wins or something? And also please be more specific as to what your error is.
And as a side note, you shouldn't be using
And as u8sand said, you're only executing things through once. Loop until the game is over, then ask to if the player wants to play again, and if so, re-loop it all.
stdio.h or stdlib.h as those aren't standard C++ headers. Use cstdio and cstdlib which are the standard C++ headers.And as u8sand said, you're only executing things through once. Loop until the game is over, then ask to if the player wants to play again, and if so, re-loop it all.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
- Hunter S. Thompson
•
•
Join Date: Jun 2009
Posts: 42
Reputation:
Solved Threads: 2
You could use a much easier type of game, here's a link from where you could get the code: http://brom8305.blogspot.com/2009/06...oe-script.html
try it out!
try it out!
// Maybe you like C++ put I prefer A++...
![]() |
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: Program won't run outside Visual Studio 2008 Express
- Next Thread: Need help on homwork assignment.
Views: 314 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment basic beginner binary c++ c/c++ calculator char class classes code command compile compiler console constructor conversion convert count data delete desktop dll dynamic encryption error file files fstream function functions game givemetehcodez graph gui homework http i/o iamthwee image input int lazy library linker list loop looping math matrix member memory newbie number numbers object objects opengl output parameter pointer pointers problem program programming project random read recursion recursive reference search sort sorting spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual visualstudio void win32 window windows winsock





