I decided to make a simple tic-tac-toe game in C++ as a small project. All help and suggestions are greatly appreciated!
main.cpp:
#include <iostream>
#include <string>
#include "TicTacToe.hpp"
TicTacToe game;
void turnPlayer1();
void turnPlayer2();
bool gameOver = false;
int main() {
std::cout << "*********************************" << std::endl;
std::cout << "Hello and welcome to tic-tac-toe!" << std::endl;
std::cout << "*********************************" << std::endl;
std::cout << "\n\n\n\n";
game.initBoard();
game.printBoard();
while(gameOver == false) {
turnPlayer1();
if(gameOver == true) {
break;
}
turnPlayer2();
}
return 0;
}
void turnPlayer1() {
bool madeTurn = false;
while(madeTurn != true) {
int x;
int y;
std::cout << "Which row do you choose player 1?" << std::endl;
std::cin >> x;
std::cout << "Which column do you choose player 1?" << std::endl;
std::cin >> y;
if(game.checkPlace(x, y)) {
game.placePiecePlayer1(x, y);
game.printBoard();
if(game.checkVictoryPlayer1()) {
std::cout << "Congrats! Player 1 has won!" << std::endl;
gameOver = true;
}
madeTurn = true;
} else {
std::cout << "Oh no! Those coordinates are not valid! Please try again." << std::endl;
madeTurn = false;
}
}
}
void turnPlayer2() {
bool madeTurn = false;
while(madeTurn != true) {
int x;
int y;
std::cout << "Which row do you choose player 2?" << std::endl;
std::cin >> x;
std::cout << "Which column do you choose player 2?" << std::endl;
std::cin >> y;
if(game.checkPlace(x, y)) {
game.placePiecePlayer2(x, y);
game.printBoard();
std::cout << game.checkVictoryPlayer2() << std::endl;
if(game.checkVictoryPlayer2()) {
std::cout << "Congrats! Player 2 has won!" << std::endl;
gameOver = true;
}
madeTurn = true;
} else {
std::cout << "Oh no! Those coordinates are not valid! Please try again." << std::endl;
madeTurn = false;
}
}
}
TicTacToe.hpp:
#ifndef TicTacToe_hpp
#define TicTacToe_hpp
#include <iostream>
#include <stdio.h>
#include <string>
class TicTacToe {
public:
TicTacToe();
void printBoard();
void placePiecePlayer1(int x, int y);
void placePiecePlayer2(int x, int y);
void initBoard();
bool checkPlace(int x, int y);
bool checkVictoryPlayer1();
bool checkVictoryPlayer2();
private:
std::string _gameBoard[4][4];
std::string _player1Piece = "[x]";
std::string _player2Piece = "[o]";
std::string _emptySpace = "[ ]";
};
#endif /* TicTacToe_hpp */
TicTacToe.cpp:
#include "TicTacToe.hpp"
TicTacToe::TicTacToe() {}
void TicTacToe::initBoard() {
for(int y = 0; y < 4; y++) {
for(int x = 0; x < 4; x++) {
if(x == 0 && y == 0) {
_gameBoard[0][0] = " 0 ";
} else if((x == 1 && y == 0) || (x == 0 && y == 1)) {
_gameBoard[y][x] = " 1 ";
} else if((x == 2 && y == 0) || (x == 0 && y == 2)) {
_gameBoard[y][x] = " 2 ";
} else if((x == 3 && y == 0) || (x == 0 && y == 3)) {
_gameBoard[y][x] = " 3 ";
} else {
_gameBoard[y][x] = _emptySpace;
}
}
}
}
void TicTacToe::printBoard() {
for(int y = 0; y < 4; y++) {
for(int x = 0; x < 4; x++) {
std::cout << _gameBoard[y][x];
}
std::cout << std::endl;
}
}
void TicTacToe::placePiecePlayer1(int x, int y) {
_gameBoard[y][x] = _player1Piece;
}
void TicTacToe::placePiecePlayer2(int x, int y) {
_gameBoard[y][x] = _player2Piece;
}
bool TicTacToe::checkPlace(int x, int y) {
bool placeOpen = true;
if(_gameBoard[y][x] != _emptySpace) {
placeOpen = false;
} else if(_gameBoard[y][x] == _emptySpace) {
placeOpen = true;
}
return placeOpen;
}
bool TicTacToe::checkVictoryPlayer1() {
bool hasWon;
if((_gameBoard[1][1] == _player1Piece) && (_gameBoard[1][2] == _player1Piece) && (_gameBoard[1][3] == _player1Piece)) {
hasWon = true; // top horizontal
} else if((_gameBoard[2][1] == _player1Piece) && (_gameBoard[2][2] == _player1Piece) && (_gameBoard[2][3] == _player1Piece)) {
hasWon = true; // middle horizontal
} else if((_gameBoard[3][1] == _player1Piece) && (_gameBoard[3][2] == _player1Piece) && (_gameBoard[3][2] == _player1Piece)) {
hasWon = true; // bottom horizontal
} else if((_gameBoard[1][1] == _player1Piece) && (_gameBoard[2][1] == _player1Piece) && (_gameBoard[3][1] == _player1Piece)) {
hasWon = true; // left vertical
} else if((_gameBoard[1][2] == _player1Piece) && (_gameBoard[2][2] == _player1Piece) && (_gameBoard[3][2] == _player1Piece)) {
hasWon = true; // middle vertical
} else if((_gameBoard[1][3] == _player1Piece) && (_gameBoard[2][3] == _player1Piece) && (_gameBoard[3][3] == _player1Piece)) {
hasWon = true; // right vertical
} else if((_gameBoard[1][1] == _player1Piece) && (_gameBoard[2][2] == _player1Piece) && (_gameBoard[3][3] == _player1Piece)) {
hasWon = true; // top left to bottom right
} else if((_gameBoard[1][3] == _player1Piece) && (_gameBoard[2][2] == _player1Piece) && (_gameBoard[1][3] == _player1Piece)) {
hasWon = true; // top right to bottom left
} else {
hasWon = false;
}
return hasWon;
}
bool TicTacToe::checkVictoryPlayer2() {
bool hasWon;
if((_gameBoard[1][1] == _player2Piece) && (_gameBoard[1][2] == _player2Piece) && (_gameBoard[1][3] == _player2Piece)) {
hasWon = true; // top horizontal
} else if((_gameBoard[2][1] == _player2Piece) && (_gameBoard[2][2] == _player2Piece) && (_gameBoard[2][3] == _player2Piece)) {
hasWon = true; // middle horizontal
} else if((_gameBoard[3][1] == _player2Piece) && (_gameBoard[3][2] == _player2Piece) && (_gameBoard[3][2] == _player2Piece)) {
hasWon = true; // bottom horizontal
} else if((_gameBoard[1][1] == _player2Piece) && (_gameBoard[2][1] == _player2Piece) && (_gameBoard[3][1] == _player2Piece)) {
hasWon = true; // left vertical
} else if((_gameBoard[1][2] == _player2Piece) && (_gameBoard[2][2] == _player2Piece) && (_gameBoard[3][2] == _player2Piece)) {
hasWon = true; // middle vertical
} else if((_gameBoard[1][3] == _player2Piece) && (_gameBoard[2][3] == _player2Piece) && (_gameBoard[3][3] == _player2Piece)) {
hasWon = true; // right vertical
} else if((_gameBoard[1][1] == _player2Piece) && (_gameBoard[2][2] == _player2Piece) && (_gameBoard[3][3] == _player2Piece)) {
hasWon = true; // top left to bottom right
} else if((_gameBoard[1][3] == _player2Piece) && (_gameBoard[2][2] == _player2Piece) && (_gameBoard[1][3] == _player2Piece)) {
hasWon = true; // top right to bottom left
} else {
hasWon = false;
}
return hasWon;
}