(Using OPENGL - simple graphics - C++) 1. The task for this project is simple, c
ID: 3812412 • Letter: #
Question
(Using OPENGL - simple graphics - C++)
1. The task for this project is simple, create a working Tic-Tac-Toe game.
2. There should be a two player mode, players take turns placing X or O symbols on a 3 × 3 grid.
3. There should also be a single player mode, where the computer should control one of the players.
4. You are free to design the graphics and the classes in any way you want. Even though we will mostly
be looking at your working program, code clarity and readability are also important.
5. The AI component does not need to be sophisticated. You will not be penalized if your AI player looses. The only requirement is that it makes valid moves.
6. Your program should be able to detect when a player has won and display an appropriate message.
Explanation / Answer
please find the code
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
struct TicTacToe {
char board[3][3];
char player1;
char cpu;
};
void initalizeGame(char boardGame[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
boardGame[i][j] = '.';
}
}
}
void printBoard(char boardGame[3][3]) {
for (int i = 0; i < 3; i++) {
cout << endl;
for (int j = 0; j < 3; j++) {
cout << " ";
cout << boardGame[i][j];
}
}
cout << endl << endl;
}
char IsWin(char boardGame[3][3]) {
if (boardGame[0][0] != '.' && (boardGame[0][0] == boardGame[0][1] && boardGame[0][0] == boardGame[0][2] ||
boardGame[0][0] == boardGame[1][0] && boardGame[0][0] == boardGame[2][0] ||
boardGame[0][0] == boardGame[1][1] && boardGame[0][0] == boardGame[2][2]))
return boardGame[0][0];
if (boardGame[1][1] != '.' && (boardGame[1][1] == boardGame[1][0] && boardGame[1][1] == boardGame[1][2] ||
boardGame[1][1] == boardGame[0][1] && boardGame[1][1] == boardGame[2][1] ||
boardGame[1][1] == boardGame[2][0] && boardGame[1][1] == boardGame[0][2]))
return boardGame[1][1];
if (boardGame[2][2] != '.' && (boardGame[2][2] == boardGame[0][2] && boardGame[2][2] == boardGame[1][2] ||
boardGame[2][2] == boardGame[2][0] && boardGame[2][2] == boardGame[2][1]))
return boardGame[2][2];
return 0;
}
int negamax(char boardGame[3][3], char player1, char player2);
int pick_best_move(char boardGame[3][3], char player1, char player2) {
int best_move_score = -9999;
int best_move_row = -9999;
int best_move_col = -9999;
int score_for_this_move = 0;
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
if (boardGame[r][c] == '.') {
boardGame[r][c] = player1;
score_for_this_move = -(negamax(boardGame, player2, player1));
boardGame[r][c] = '.';
if (score_for_this_move >= best_move_score) {
best_move_score = score_for_this_move;
best_move_row = r;
best_move_col = c;
}
}
}
}
return (10*best_move_row + best_move_col);
}
int negamax(char boardGame[3][3], char player1, char player2) {
int best_move_score = -9999;
int score_for_this_move = 0;
//If player 1 wins, then the score is high (good for player1)
if (IsWin(boardGame) == player1)
return 1000;
//If player 2 loses, then the score is low (bad for player1)
else if (IsWin(boardGame) == player2)
return -1000;
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
if (boardGame[r][c] == '.') {
boardGame[r][c] = player1; //Try test move.
score_for_this_move = -(negamax(boardGame, player2, player1));
boardGame[r][c] = '.';
if (score_for_this_move >= best_move_score) {
best_move_score = score_for_this_move;
}
}
}
}
if (best_move_score == -9999 || best_move_score == 0)
return 0;
else if (best_move_score < 0)
return best_move_score + 1;
else if (best_move_score > 0)
return best_move_score - 1;
}
void player_placement(char boardGame[3][3], char player) {
while (1) {
string string_row, string_col;
int row = 0, col = 0;
while (1) {
cout << "Where would you like to play? " << endl << "Enter the row: ";
cin >> string_row;
row = atoi(string_row.c_str());
if (row >= 1 && row <= 3)
break;
cout << "You need to enter a row on the board (between 1 and 3, inclusive)." << endl;
}
while (1) {
cout << "Enter the column: ";
cin >> string_col;
col = atoi(string_col.c_str());
if (col >= 1 && col <= 3)
break;
cout << "You need to enter a column on the board (between 1 and 3, inclusive)." << endl;
}
if (boardGame[row-1][col-1] == '.') {
boardGame[row-1][col-1] = player;
break;
}
else
cout << "This position is already occupied" << endl << endl;
}
}
char determine_cpu_choice (char player1) {
char cpu_char;
cout << "Hello, I am the computer you will be playing." << endl;
cout << "If you chose x's, I will be o's. If you chose o's, I will be x's." << endl;
cout << "If you chose neither x or o, I will default to x." << endl << endl;
if (player1 == 'x' || player1 == 'X')
cpu_char = 'o';
else
cpu_char = 'x';
return cpu_char;
}
char determine_player_choice (string s) {
while (1) {
string choice;
cout << s << ": What would you like your character to be? ";
cin >> choice;
if (choice.size() > 1) {
cout << "You inputted more than one character. Please try again." << endl;
continue;
}
cout << endl;
return choice[0];
}
}
void PlayGame(char board[3][3], char player1, char cpu) {
int moves = 0;
while (moves < 9) {
player_placement(board, player1);
moves++;
printBoard(board);
if (IsWin(board)) {
cout << player1 << " won!" <<endl;
exit(1);
}
if (moves == 9)
break;
int where_to_move = pick_best_move(board, cpu, player1);
int row = where_to_move / 10;
int col = where_to_move % 10;
board[row][col] = cpu;
moves++;
printBoard(board);
if (IsWin(board)) {
cout << cpu << " won!" <<endl;
cout << "If I were you, I would've played at: " << "(row " << where_to_move/10 + 1 << ", col " << where_to_move % 10 + 1 << ")" << endl;
exit(2);
}
}
cout << "Match is Draw" << endl;
}
int main() {
TicTacToe game;
initalizeGame(game.board);
game.player1 = determine_player_choice("Player 1");
game.cpu = determine_cpu_choice(game.player1);
printBoard(game.board);
PlayGame(game.board, game.player1, game.cpu);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.