| | |
loop problem
![]() |
•
•
Join Date: Sep 2005
Posts: 3
Reputation:
Solved Threads: 0
Hi all
I'm new to this site and hoping someone can assist with this problem. I've done all the code for the Tic Tac Toe game but having problems placing the 'o's & 'x's. This is the part of code I'm stuck on. Any help is greatly appreciated.
I'm new to this site and hoping someone can assist with this problem. I've done all the code for the Tic Tac Toe game but having problems placing the 'o's & 'x's. This is the part of code I'm stuck on. Any help is greatly appreciated.
C Syntax (Toggle Plain Text)
char PLAYER_TURN() { int i; int x,y; board[9]; drawX(x,y); do { displayBoard(); for (i = 0; i<9;i++); while (board[i] ==! -1) { i++; } board[i]=0; } return 0; }
Last edited by Dave Sinkula; Sep 16th, 2005 at 4:36 pm. Reason: Added [code][/code] tags.
•
•
Join Date: Sep 2005
Posts: 3
Reputation:
Solved Threads: 0
Here is my code - apologies if I haven't posted it correctly!! I'm having problems displaying the 'O's & 'X's.
<< moderator edit: added code tags: [code][/code] >>
C Syntax (Toggle Plain Text)
#include <iostream.h> #include <stdio.h> #include <conio.h> char board[9]; int TURN = 0; int MOVE; //functions void instructions(); int displayBoard(); void hline (int x, int y, int col); void vline (int x, int y, int col); void playgame(); char PLAYER_TURN(); char CPU_TURN(); void drawX(int x, int y); void drawO(int x, int y); int checkWinner(); int main() { clrscr(); char key; //define a character called key do { cout << "\n\n\n\n\n\n\t\t\t\tTIC TAC TOE\n\n\n\t\t"; cout << "Think You Can Win a Game of Tic Tac Toe?\n\n\n\t\t"; cout << "Please choose one of the following options...\n\n\n"; cout << "\n\tView Instructions Press '1'"; cout << "\n\tStart New Game Press '2'"; cout << "\n\tExit Program Press '3'"; do { key= getch();//input choice } while (key!='1' && key!='2' && key!='3'); if (key=='1') { instructions(); } if (key=='2') { playgame(); } } while (key!='3'); } void instructions() { clrscr(); cout << " \n\tPlayer 1 always is always X"; cout << "\n\n\tThe object of the game is to get 3 matching symbols in a row\n\n\tThe grid is numbered as shown below...."; cout << "Press a key to continue..."; gotoxy(20,15); cout << "0 | 1 | 2\n"; gotoxy(20,16); cout << "---------\n"; gotoxy(20,17); cout << "3 | 4 | 5\n"; gotoxy(20,18); cout << "---------\n"; gotoxy(20,19); cout << "6 | 7 | 8"; getch(); clrscr(); } void playgame() { int i; for (i=0;i<10;i++) board[i]= -1; PLAYER_TURN(); CPU_TURN(); PLAYER_TURN(); CPU_TURN(); PLAYER_TURN(); checkWinner(); CPU_TURN(); checkWinner(); PLAYER_TURN(); checkWinner(); CPU_TURN(); checkWinner(); PLAYER_TURN(); checkWinner(); } int displayBoard() { int i; clrscr(); hline(5,10,17); hline(5,16,17); vline(10,5,17); vline(16,5,17); for (i = 0; i<9;i++); { if (i == 0) { drawX(5,5); } else if (i == 1) { drawX (11,5); } else if (i == 2) { drawX (17,5); } else if (i == 3) { drawX (5,11); } else if (i == 4) { drawX (11,11); } else if (i == 5) { drawX (17,11); } else if (i == 6) { drawX (5,17); } else if (i == 7) { drawX (11,17); } else if (i == 8) { drawX (17,17); } } { if (i == 0) { drawO (5,5); } else if (i == 1) { drawO (11,5); } else if (i == 2) { drawO (17,5); } else if (i == 3) { drawO (5,11); } else if (i == 4) { drawO (11,11); } else if (i == 5) { drawO (17,11); } else if (i == 6) { drawO (5,17); } else if (i == 7) { drawO (11,17); } else if (i == 8) { drawO (17,17); } return 0; } } void hline (int x, int y, int col) { int i; textcolor (col); for (i=0;i<17;i++) { gotoxy(i+x,y); cprintf("%c",219); } } void vline (int x, int y, int col) { int i; textcolor (col); for (i=0;i<17;i++) { gotoxy(x,i+y); cprintf("%c",219); } } void drawX (int x, int y) { gotoxy(x,y+1); cprintf(" X X"); gotoxy(x,y+2); cprintf(" X "); gotoxy(x,y+3); cprintf(" X X"); } void drawO (int x, int y) { gotoxy(x,y+1); cprintf(" O "); gotoxy(x,y+2); cprintf(" O O"); gotoxy(x,y+3); cprintf(" O "); } char PLAYER_TURN() { int i; int x,y; board[9]; drawX(x,y); displayBoard(); { for (i = 0; i<10;i++); { while(board[i] ==! -1); } checkWinner(); } return 0; } char CPU_TURN() { int i; int x,y; board[9]; drawO(x,y); displayBoard(); for (i = 0; i<10;i++); { board[i] == -1; } checkWinner(); } int checkWinner() { while(TURN < 10) { if (board[0]== 'X' && board[4]== 'X' && board[8]== 'X') {cout<<"Player 1 is the winner";break;} if (board[6]== 'X' && board[4]== 'X' && board[2]== 'X') {cout<<"Player 1 is the winner";break;} if (board[0]== 'X' && board[3]== 'X' && board[6]== 'X') {cout<<"Player 1 is the winner";break;} if (board[1]== 'X' && board[4]== 'X' && board[7]== 'X') {cout<<"Player 1 is the winner";break;} if (board[2]== 'X' && board[5]== 'X' && board[8]== 'X') {cout<<"Player 1 is the winner";break;} if (board[0]== 'X' && board[1]== 'X' && board[2]== 'X') {cout<<"Player 1 is the winner";break;} if (board[3]== 'X' && board[4]== 'X' && board[5]== 'X') {cout<<"Player 1 is the winner";break;} if (board[6]== 'X' && board[7]== 'X' && board[8]== 'X') {cout<<"Player 1 is the winner";break;} // Check for Player 2... if (board[0]== 'O' && board[4]== 'O' && board[8]== 'O') {cout<<"Player 2 is the winner";break;} if (board[6]== 'O' && board[4]== 'O' && board[2]== 'O') {cout<<"Player 2 is the winner";break;} if (board[0]== 'O' && board[3]== 'O' && board[6]== 'O') {cout<<"Player 2 is the winner";break;} if (board[1]== 'O' && board[4]== 'O' && board[7]== 'O') {cout<<"Player 2 is the winner";break;} if (board[2]== 'O' && board[5]== 'O' && board[8]== 'O') {cout<<"Player 2 is the winner";break;} if (board[0]== 'O' && board[1]== 'O' && board[2]== 'O') {cout<<"Player 2 is the winner";break;} if (board[3]== 'O' && board[4]== 'O' && board[5]== 'O') {cout<<"Player 2 is the winner";break;} if (board[6]== 'O' && board[7]== 'O' && board[8]== 'O') {cout<<"Player 2 is the winner";break;} } }
Last edited by Dave Sinkula; Sep 16th, 2005 at 4:37 pm. Reason: Added [code][/code] tags.
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Solved Threads: 5
I'm a bit busy at work, so I don't have time to run through this completely.
So, what's the problem? The X's and O's don't show up at all?
Oh, and you may find this helpful to try:
;
That creates a 3x3 grid that you can use in a more reasonable fashion. I think it's x, then y, but the origin would most logically be in the upper left corner, like this:
With the horizontal and vertical bars just for the purpose of illustration, not as code.
I think your code is a bit too complex, personally. I don't know why I feel that way, but my intuition (admittedly poor) just tells me that you're overdefining things.
So, what's the problem? The X's and O's don't show up at all?
Oh, and you may find this helpful to try:
C Syntax (Toggle Plain Text)
char board[3][3];
That creates a 3x3 grid that you can use in a more reasonable fashion. I think it's x, then y, but the origin would most logically be in the upper left corner, like this:
C Syntax (Toggle Plain Text)
board[0][0] | board[0][1] | board[0][2] ------------------------------------- board[1][0] | board[1][1] | board[1][2] ------------------------------------- board[2][0] | board[2][1] | board[2][2]
With the horizontal and vertical bars just for the purpose of illustration, not as code.
I think your code is a bit too complex, personally. I don't know why I feel that way, but my intuition (admittedly poor) just tells me that you're overdefining things.
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
assuming EX and OH are defined somewhere as non-zero ints then something like this can keep your checking routines simple.
this assumes GameGrid_ is defined as a an array of 9 ints.
Similarly your drawing routines can be simplified but ill leave you to work that out on your own.
C Syntax (Toggle Plain Text)
int MainWindow::IsWinner() { // this holds the patterns we will check for static int pattern[8][3]= { 0,1,2, 3,4,5, 6,7,8, 0,3,6, 1,4,7, 2,5,8, 0,4,8, 2,4,6 }; // this loop does the checking for( int i = 0; i < 8; ++i) { if( (GameGrid_[pattern[i][0]] == EX) && (GameGrid_[pattern[i][1]] == EX) && (GameGrid_[pattern[i][2]] == EX)) return EX; if( (GameGrid_[pattern[i][0]] == OH) && (GameGrid_[pattern[i][1]] == OH) && (GameGrid_[pattern[i][2]] == OH)) return OH; } // no winner so return 0 return 0; }
Similarly your drawing routines can be simplified but ill leave you to work that out on your own.
![]() |
Similar Threads
- nested for loop problem (Java)
- Alright this loop problem is bugging me (Java)
- I think Loop problem (Java)
- Cold Fusion Loop problem (ColdFusion)
- Help w/ for loop. (C++)
- Random Different Number in each loop ?? HLP Me PLZ (c or c++) (C++)
- loop problem (C++)
Other Threads in the C Forum
- Previous Thread: Give me advice
- Next Thread: Pert/
| Thread Tools | Search this Thread |
* adobe ansi api array arrays bash binarysearch calculate centimeter char cm convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hardware highest homework i/o ide inches initialization intmain() iso km linked linkedlist linux linuxsegmentationfault list logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test testautomation unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h





