I am doing C++ for over a year and this is my game I tried making tic-tac-toe. And it's also my first program that uses multiple source files.
main.cpp
#include"Header.h"
#include"GUI.h"
#include"UI.h"
int main()
{
int* board = new int[9] {0, 0, 0, 0, 0, 0, 0, 0, 0};
// 1 2 3
// 4 5 6
// 7 8 9
int place{ 0 }; // /\
// || -1
bool turn{ false }; // true = O, false = X
bool win{ false }; // flase = no win, true = win
int count{ 0 }; // counts ammount of moves
while (!win && count < 9)
{
place = question(board) - 1;
system("cls");
turn = change_board(board, turn, place);
switch (check_win(board))
{
case 0:
break;
case 1:
system("cls");
print(board);
change_color(3, 0);
cout << "\nPlayer Two won\n";
win = !win;
break;
case 2:
system("cls");
print(board);
change_color(4, 0);
cout << "\nPlayer one won\n";
win = !win;
break;
}
count++;
}
if (!win)
{
system("cls");
print(board);
change_color(2, 0);
cout << "\nIt's a draw\n";
}
change_color(7, 0);
return 0;
}
Header.h
using namespace std;
#include <iostream>
#include<windows.h>
GUI.h
void change_color(int text, int background);
void print(int* board);
int question(int* board);
GUI.cpp
#include"Header.h"
char symbols[3]{ ' ', 'X', 'O' };
void change_color(int text, int background)
{
HANDLE hConsole;
int color{ text + 16 * background };
//0 = Black 8 = Gray
//1 = Blue 9 = Light Blue
//2 = Green a = Light Green
//3 = Aqua b = Light Aqua
//4 = Red c = Light Red
//5 = Purple d = Light Purple
//6 = Yellow e = Light Yellow
//7 = White f = Bright White
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, color);
}
void color_print(char symbol)
{
switch (symbol)
{
case 'X':
change_color(4, 0);
cout << symbol;
change_color(7, 0);
break;
case 'O':
change_color(3, 0);
cout << symbol;
change_color(7, 0);
break;
default:
cout << ' ';
break;
}
return;
}
void color_print(int symbol, int place)
{
switch (symbol)
{
case 1:
change_color(4, 0);
cout << 'X';
change_color(7, 0);
break;
case 2:
change_color(3, 0);
cout << 'O';
change_color(7, 0);
break;
default:
change_color(2, 0);
cout << place + 1;
change_color(7, 0);
break;
}
return;
}
void print(int* board)
{
for (int column{ 0 }; column < 2; column++)
{
for (int row{ 0 };row<2;row++)
{
cout << " "; color_print(symbols[board[3 * column + row]]); cout << " |";
}
cout << " "; color_print(symbols[board[3 * column + 2]]); cout << endl;
cout << "-----------------\n";
}
for (int row{ 0 }; row < 2; row++)
{
cout << " "; color_print(symbols[board[row + 6]]); cout << " |";
}
cout << " "; color_print(symbols[board[8]]); cout << endl;
return;
}
void print_to_ask(int* board)
{
for (int column{ 0 }; column < 2; column++)
{
for (int row{ 0 }; row < 2; row++)
{
cout << " "; color_print(board[3 * column + row], 3 * column + row); cout << " |";
}
cout << " "; color_print(board[3 * column + 2], 3 * column + 2); cout << endl;
cout << "-----------------\n";
}
for (int row{ 0 }; row < 2; row++)
{
cout << " "; color_print(board[row + 6], row + 6); cout << " |";
}
cout << " "; color_print(board[8], 8); cout << endl;
return;
}
int question(int* board)
{
int place{ 0 };
print_to_ask(board);
cout << "\n\n Please, input the place where you want to do your turn: "; cin >> place;
if (place < 1 || place > 9 || board[place - 1] != 0)
{
system("cls");
change_color(0, 4);
cout << "Invalid option\nPlease retry\n\n";
change_color(7, 0);
place = question(board);
}
return place;
}
UI.h
bool change_board(int* board, bool turn, int place);
int check_win(int* board );
UI.cpp
#include"Header.h"
bool change_board(int* board, bool turn, int place)
{
board[place] = int(turn) + 1;
return !turn;
}
bool check_win_side(int* board, int side)
{
for (int x{ 0 }; x < 3; x++)
{
if (board[x] == board[x + 3] && board[x] == board[x + 6] && board[x] == side)
return true;
}
for (int x{ 0 }; x < 3; x++)
{
if (board[x * 3] == board[x * 3 + 1] && board[x * 3] == board[x * 3 + 2] && board[x * 3] == side)
return true;
}
if ((board[0] == board[4] && board[0] == board[8] && board[0] && board[0] == side) || (board[2] == board[4] && board[2] == board[6] && board[2] == side))
{
return true;
}
return false;
}
int check_win(int*board)
{
if (check_win_side(board, 1))
{
return 2;
} else if (check_win_side(board, 2))
{
return 1;
}
return 0;
}