Public Group

# C++ Tic Tac Toe

This topic is 2563 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

#include<iostream> using namespace std; void printBoard(char array[3][3]); void resetBoard(char array[3][3]); void playerInput(char tempArray[3][3]); //forward declarations int main() { char board[3][3]; bool gameWon = false; //Declared variables //Board setup resetBoard(board); printBoard(board); cout << "Please enter the designated letter corrisponding to the location you want to select. " << endl; //Begin game loop do{ playerInput(board); //checkWin(gameWon); //printBoard(board); }while(gameWon=false); printBoard(board); } void resetBoard(char array[3][3]){ for(int i = 0;i <=3;i++){ array[0] = '*'; } for(int i = 0;i <=3;i++){ array[1] = '*'; } for(int i = 0;i <=3;i++){ array[2] = '*'; } } //Prints the board on the screen. void printBoard(char tempArray[3][3]){ char left = 'y'; char middle = 'n'; char right = 'n'; int incriment = 0,incrimentTwo = 0; for(int a = 0;a <3;a++){ for(int i = 0;i <3;i++){ while(incriment==0){ cout << " " << tempArray;//these while statements draw the board out middle = 'y'; left = 'n'; incriment = 1; } while(incriment==1){ cout << "|" << tempArray << "|"; middle = 'n'; right = 'y'; incriment = 2; } while(incriment==2){ cout << tempArray; incriment = 3; } } cout << endl; incriment = 0;//incriment is set back to 0 so when it goes through the for loop again it checks all while loops. Once the for loops run out it causes this not to run for an infinite cycle. if(incrimentTwo < 2){ cout << "=======" << endl;//this prints the bars onto the board without printing anything on the bottom. incrimentTwo++; } } } //This function sets the letter each player uses, then it gets the input from the user and puts it into the array. void playerInput(char tempArray[3][3]){ char playerPiece; int playerChoice; bool spotTaken = false; int turnNumber = 0; if(turnNumber % 2 == 0){ playerPiece = 'x'; }else{ playerPiece = 'y'; } cout << "Enter your choice" << endl; cout << "1" << "|" << "2" << "|" << "3" << endl; cout << "=======" << endl; cout << "4" << "|" << "5" << "|" << "6" << endl; cout << "=======" << endl; cout << "7" << "|" << "8" << "|" << "9" << endl; cin >> playerChoice; switch(playerChoice){ case 1: if(tempArray[0][0] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[0][0] = playerPiece; break; } case 2: if(tempArray[0][1] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[0][1] = playerPiece; break; } case 3: if(tempArray[0][2] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[0][2] = playerPiece; break; } case 4: if(tempArray[1][0] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[1][0] = playerPiece; break; } case 5: if(tempArray[1][1] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[1][1] = playerPiece; break; } case 6: if(tempArray[1][2] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[1][2] = playerPiece; break; } case 7: if(tempArray[2][0] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[2][0] = playerPiece; break; } case 8: if(tempArray[2][1] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[2][1] = playerPiece; break; } case 9: if(tempArray[2][2] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[2][2] = playerPiece; break; } default: cout << "Error" << endl; } if(spotTaken == true){ playerInput(tempArray); }else{ turnNumber++; } } 
The problem I'm having is when I enter 1 into the loop, it sets all the array elements to x. (when it should obviously only set one). The second problem is when I enter 2-9 I get no input into the variables at all (the characters stay *

##### Share on other sites
The problem is in the switch statement:

[color=#000088]switch[color=#666600]([color=#000000]playerChoice[color=#666600]){[color=#000000]
[color=#000088]case[color=#000000] [color=#006666]1[color=#666600]:[color=#000000]
[color=#000088]if[color=#666600]([color=#000000]tempArray[color=#666600][[color=#006666]0[color=#666600]][[color=#006666]0[color=#666600]][color=#000000] [color=#666600]==[color=#000000] playerPiece[color=#666600]){[color=#000000]
cout [color=#666600]<<[color=#000000] [color=#008800]"Spot taken, choose another."[color=#000000] [color=#666600]<<[color=#000000] endl[color=#666600];[color=#000000]
spotTaken [color=#666600]=[color=#000000] [color=#000088]true[color=#666600];[color=#000000]
[color=#666600]}[color=#000088]else[color=#666600]{[color=#000000]
tempArray[color=#666600][[color=#006666]0[color=#666600]][[color=#006666]0[color=#666600]][color=#000000] [color=#666600]=[color=#000000] playerPiece[color=#666600];[color=#000000]
[color=#000088]break[color=#666600];[color=#000000]
[color=#666600]}[color=#000000]
[color=#000088]case[color=#000000] [color=#006666]2[color=#666600]:

If you look closely, the "break" instruction is inside the "else" only. It should be outside the if/else.

##### Share on other sites

The problem is in the switch statement:

[color="#000088"]switch[color="#666600"]([color="#000000"]playerChoice[color="#666600"]){ [color="#000088"]case [color="#006666"]1[color="#666600"]: [color="#000088"]if[color="#666600"]([color="#000000"]tempArray[color="#666600"][[color="#006666"]0[color="#666600"]][[color="#006666"]0[color="#666600"]] [color="#666600"]==[color="#000000"] playerPiece[color="#666600"]){[color="#000000"]
cout [color="#666600"]<< [color="#008800"]"Spot taken, choose another." [color="#666600"]<<[color="#000000"] endl[color="#666600"];[color="#000000"]
spotTaken [color="#666600"]= [color="#000088"]true[color="#666600"]; [color="#666600"]}[color="#000088"]else[color="#666600"]{[color="#000000"]
tempArray[color="#666600"][[color="#006666"]0[color="#666600"]][[color="#006666"]0[color="#666600"]] [color="#666600"]=[color="#000000"] playerPiece[color="#666600"]; [color="#000088"]break[color="#666600"]; [color="#666600"]} [color="#000088"]case [color="#006666"]2[color="#666600"]:

If you look closely, the "break" instruction is inside the "else" only. It should be outside the if/else.

Thanks, I didn't even think about that being a problem. It still looks like i'm having the same problems tho.

##### Share on other sites
Have you tried stepping through with a debugger?

And if you keep having trouble, can you post your updated code?

##### Share on other sites

Have you tried stepping through with a debugger?

And if you keep having trouble, can you post your updated code?

I actually have never used a debugger could you recommend one?

Updated after the break fixes.
#include<iostream> using namespace std; void printBoard(char array[3][3]); void resetBoard(char array[3][3]); void playerInput(char tempArray[3][3]); //forward declarations int main() { char board[3][3]; bool gameWon = false; //Declared variables //Board setup resetBoard(board); printBoard(board); cout << "Please enter the designated letter corrisponding to the location you want to select. " << endl; //Begin game loop do{ playerInput(board); //checkWin(gameWon); //printBoard(board); }while(gameWon=false); printBoard(board); } void resetBoard(char array[3][3]){ for(int i = 0;i <=3;i++){ array[0] = '*'; } for(int i = 0;i <=3;i++){ array[1] = '*'; } for(int i = 0;i <=3;i++){ array[2] = '*'; } } //Prints the board on the screen. void printBoard(char tempArray[3][3]){ char left = 'y'; char middle = 'n'; char right = 'n'; int incriment = 0,incrimentTwo = 0; for(int a = 0;a <3;a++){ for(int i = 0;i <3;i++){ while(incriment==0){ cout << " " << tempArray;//these while statements draw the board out middle = 'y'; left = 'n'; incriment = 1; } while(incriment==1){ cout << "|" << tempArray << "|"; middle = 'n'; right = 'y'; incriment = 2; } while(incriment==2){ cout << tempArray; incriment = 3; } } cout << endl; incriment = 0;//incriment is set back to 0 so when it goes through the for loop again it checks all while loops. Once the for loops run out it causes this not to run for an infinite cycle. if(incrimentTwo < 2){ cout << "=======" << endl;//this prints the bars onto the board without printing anything on the bottom. incrimentTwo++; } } } //This function sets the letter each player uses, then it gets the input from the user and puts it into the array. void playerInput(char tempArray[3][3]){ char playerPiece; int playerChoice; bool spotTaken = false; int turnNumber = 0; if(turnNumber % 2 == 0){ playerPiece = 'x'; }else{ playerPiece = 'y'; } cout << "Enter your choice" << endl; cout << "1" << "|" << "2" << "|" << "3" << endl; cout << "=======" << endl; cout << "4" << "|" << "5" << "|" << "6" << endl; cout << "=======" << endl; cout << "7" << "|" << "8" << "|" << "9" << endl; cin >> playerChoice; switch(playerChoice){ case 1: if(tempArray[0][0] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[0][0] = playerPiece; } break; case 2: if(tempArray[0][1] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[0][1] = playerPiece; } break; case 3: if(tempArray[0][2] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[0][2] = playerPiece; } break; case 4: if(tempArray[1][0] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[1][0] = playerPiece; } break; case 5: if(tempArray[1][1] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[1][1] = playerPiece; } break; case 6: if(tempArray[1][2] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[1][2] = playerPiece; } break; case 7: if(tempArray[2][0] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[2][0] = playerPiece; } break; case 8: if(tempArray[2][1] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[2][1] = playerPiece; } break; case 9: if(tempArray[2][2] == playerPiece){ cout << "Spot taken, choose another." << endl; spotTaken = true; }else{ tempArray[2][2] = playerPiece; } break; default: cout << "Error" << endl; } if(spotTaken == true){ playerInput(tempArray); }else{ turnNumber++; } } 

##### Share on other sites
Your printBoard function is the culprit. The input function performs fine but the print function either modifies or just display the board wrong.

The compiler gives me reminders that left, middle, and right are never used for anything meaningful. You also have a weird use of a while statement XD I believe an if statement is better suited for the job.

That being said, I'm not sure why they are there at all. You don't need the increment variables because the a and i are already keeping track of the index. However, you're not accessing the proper index in the board array. Every time the board is accessed in that function, you are accessing element [0,0] because in the second for loop, you run through all the while statements on the first iteration and they get blocked on the remaining two, while instead of using a to index a row, you use i which resets every time these statements execute.

This function is possible to implement with only two variables: a and i that run the loops. Try doing it that way.

##### Share on other sites
I know it's somthing to do with it, I don't know what tho, that was what the question was.

##### Share on other sites
I note that in your printBoard function you are writing tempArray to the screen, which means that you are only going to write tempArray[0][0], tempArray[1][1] and tempArray[2][2] to the screen. You probably want to write tempArray[a] to the screen (and possibly use more descriptive variable names, such as x and y).

##### Share on other sites

I note that in your printBoard function you are writing tempArray to the screen, which means that you are only going to write tempArray[0][0], tempArray[1][1] and tempArray[2][2] to the screen. You probably want to write tempArray[a] to the screen (and possibly use more descriptive variable names, such as x and y).

you're a life saver, I don't know what I was thinking in writing a multidimensional array and only using one variable.

Wow I actually had made the two variables just never put them in there for some reason. Me asking dumb questions *facepalm*

##### Share on other sites

[color="#000088"]void[color="#000000"] resetBoard[color="#666600"]([color="#000088"]char[color="#000000"] array[color="#666600"][[color="#006666"]3[color="#666600"]][[color="#006666"]3[color="#666600"]])
[color="#666600"]{
[color="#000088"] for[color="#666600"]([color="#000088"]int[color="#000000"] i [color="#666600"]= [color="#006666"]0[color="#666600"];[color="#000000"]i [color="#666600"]<=[color="#006666"]3[color="#666600"];[color="#000000"]i[color="#666600"]++)
[color="#666600"] {[color="#000000"]
array[color="#666600"][[color="#000000"]i[color="#666600"]][[color="#006666"]0[color="#666600"]] [color="#666600"]= [color="#008800"]'*'[color="#666600"];
[color="#666600"] }
[...]
[/quote]

Hello, in your resetBoard function, the for loops actually iterate four times, i=0,1,2,3,
then assigning a value for (non-existing) array[3][n].
This will in best case produce an error, and in worst case undefined behaviour.
Use for (int i = 0; i < 3 ; i++) to loop three times: 0,1,2

Cheers,
Daniel

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633702
• Total Posts
3013448
×