patterncppMinor
Tic Tac Toe in C++
Viewed 0 times
tactictoe
Problem
Here is my two-player Tic Tac Toe game. How can I improve it?
#include
#include
//Simple Display
void Display(std::vector const &grid){
//Creating a onscreen grid
std::cout const& grid){
if(grid[position] == '-')
return false;
else
return true;
}
void Turn(std::vector &grid, char player){
int row = 0;
char column = 0;
int position = 0;
bool check = true;
std::cout > row;
std::cout > column;
position = 3*(column-'A')+(row-1);
if(!Used(position,grid)){
check = false;
}
else{
std::cout const& grid, char player){
for(int a = 0; a grid(9,'-');
while(true){
Display(grid);
Turn(grid, 'X');
if(Win(grid, 'X')){
Display(grid);
std::cout << "\nX is the Winner!";
break;
}
Display(grid);
Turn(grid,'O');
if(Win(grid, 'O')){
Display(grid);
std::cout << "\nO is the Winner!";
break;
}
}
}Solution
I'll just review what you have here, although something like this would work better with classes (you could always attempt this implementation later).
-
Your vector isn't quite grid-like as it's only one-dimensional. It may also be why
Either way, you just need an
You first need to include `
You should also have a way of choosing whether to start with X or O. Normally, someone will choose to go first, and they will choose their own symbol.
-
Your vector isn't quite grid-like as it's only one-dimensional. It may also be why
Display() is a bit confusing, which shouldn't be the case with a 2D structure.Either way, you just need an
std::array since the board's size is fixed:std::array, 3> grid;You first need to include `
, and you can then remove .
-
In bool Used(), this:
if(grid[position] == '-')
return false;
else
return true;
can simply become this:
return !(grid[position] == '-');
The statement already gives a conditional result, so you just have to return it.
Also, you don't need to pass position by const& as it's a native type. Just pass by value, with const if you want to be safe.
-
Don't just list variables at the start of Turn() (or in general). Declare or initialize them as close to their use as possible.
For instance, here's what it should be for the user input portion:
std::cout > row;
std::cout > column;
Side-note: row and column don't need be initialized; they can just be declared.
Consider having input validation in case the user inputs a valid board spot, otherwise the program will stop working and the game will have to be restarted.
You could also simplify the function and the input validation by using either letters or numbers for both row and column. Using different ones makes the code and interface more confusing.
-
This:
while(check == true)
is the same as this but more concise:
while(check)
For false, use !:
while(!check)
-
The while loop in main() is quite repetitive. You could just have a bool` assigned to the winning player, then display that value in the winning (or losing) message after the loop.You should also have a way of choosing whether to start with X or O. Normally, someone will choose to go first, and they will choose their own symbol.
Code Snippets
std::array<std::array<char, 3>, 3> grid;if(grid[position] == '-')
return false;
else
return true;return !(grid[position] == '-');std::cout << "Row(1,2,3): ";
int row;
std::cin >> row;
std::cout << player << ": Column(A,B,C): ";
char column = 0;
std::cin >> column;while(check == true)Context
StackExchange Code Review Q#47609, answer score: 5
Revisions (0)
No revisions yet.