Question

In: Computer Science

Here is an interaction in a tic-tac-toe game, with user input in bold: >> gm =...

Here is an interaction in a tic-tac-toe game, with user input in bold:

>> gm = Game.new('andy', 'mike')
=> #<Game:0x2e91d78 @players=[Andy, Mike]>

>> gm.play_game('mike') Mike, enter your next O O--
---

---
Andy, enter your next X
O-X
---
---
Mike, enter your next O
O-X
-O-
---
Andy, enter your next X move
Bad move dude! You go again.
O-X
-O-
---
Andy, enter your next X move
O-X
-OX
---
Mike, enter your next O move
O-X
-OX
--O
Mike, you won!
=> nil
>> gm.play_game('karen')
I don't know that player. Please try again. => nil

>> gm.play_game('andy')
Andy, enter your next O move (1-9): 5 ---
-O-
---

When a new game is created, two players register with it by name (the arguments to Game.new). The play_game message is called with one of the player’s names and starts a new game where that player plays first using the O marker. (A game can be sent repeated play_game messages so the same pair of players can play multiple games). In each iteration, the current player is prompted for the position of her move and then she enters the move number, where the nine squares are numbered in row-major order from 1 through 9. If a move is illegal (i.e., already occupied), the current player is scolded and prompted again for a move. After each move, the board is redrawn. A game ends when either one of the players forms a horizontal, vertical, or diagonal row of her three markers (in the usual way), or the board is full indicating a tie.

The assignment is not to implement this game in Ruby. Rather, the assignment is to develop sequence diagrams to discover the objects, their responsibilities, and the messages they respond to. Specifically, focus on these two scenarios:

  1. Where the current game is not over, the current player is prompted for a legal move which she supplies and then the move is made and the updated game board is displayed.

  2. A new game is started and then play iterates between the two players until one of the players wins.

Note that the first scenario is lower level than and fits into the second scenario.

Solutions

Expert Solution

Please give positive ratings for my effort. Thanks.

PROGRAM

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

enum Action {YES = 0, NO, X_WIN, O_WIN};

const string red("\033[0;31m");
const string cyan("\033[0;36m");
const string reset("\033[0m");

const unsigned int EMPTY_SUM = 216; // ascii value of an "empty" slot
const unsigned int X_SUM = 272 + 434 + 275; // ascii value of an slot filled by X (also red + reset)
const unsigned int O_SUM = 263 + 439 + 275; // ascii value of an slot filled by O (also cyan + reset)

const string X_PLAYER = red + "X" + reset;
const string O_PLAYER = cyan + "O" + reset;

string board[][5] = {" ", " 0 ", " 1 ", " 2 ", "(y)",
"0 ", "[ ]", "[ ]", "[ ]", " ",
"1 ", "[ ]", "[ ]", "[ ]", " ",
"2 ", "[ ]", "[ ]", "[ ]", " ",
"(x)", " ", " ", " ", " "};

bool running = true;

// clears the screen and displays the current board
void print_board() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
cout << endl;
}

// returns the ascii value of the given string
int get_ascii_value(string &str) {
int ascii_count = 0;
for (int i = 0; i < str.length(); i++) {
ascii_count += int(str.at(i));
}
return ascii_count;
}

// checks the sum of the collindant slots to see if an slot is surrounded by the same values (aka: wins)
Action check_sum(int sum, string player) {
if (sum == X_SUM * 2) {
if (player == X_PLAYER)
return X_WIN;
else if (player == O_PLAYER)
return YES;
} else if (sum == O_SUM * 2) {
if (player == O_PLAYER)
return O_WIN;
else if (player == X_PLAYER)
return YES;
} else {
return YES;
}
}

// checks the collindant slots in order to see if a player has won, is a valid movement or is not a valid movement
Action get_action_from_movement(unsigned short *x, unsigned short *y, string player) {
if (*x > 3 || *y > 3 || *x < 0 || *y < 0)
return NO;

if (get_ascii_value(board[*x][*y]) != EMPTY_SUM)
return NO;

Action curr_state = NO;

if (*x == 1) {
curr_state = check_sum(get_ascii_value(board[2][*y]) + get_ascii_value(board[3][*y]), player);
if (curr_state == YES && *y == 1) {
curr_state = check_sum(get_ascii_value(board[2][2]) + get_ascii_value(board[3][3]), player);
} else if (curr_state == YES && *y == 3) {
curr_state = check_sum(get_ascii_value(board[2][2]) + get_ascii_value(board[3][1]), player);
}
} else if (*x == 2) {
curr_state = check_sum(get_ascii_value(board[1][*y]) + get_ascii_value(board[3][*y]), player);
if (curr_state == YES && *y == 2) {
curr_state = check_sum(get_ascii_value(board[1][1]) + get_ascii_value(board[3][3]), player);
if (curr_state == YES) {
curr_state = check_sum(get_ascii_value(board[3][1]) + get_ascii_value(board[1][3]), player);
}
}
} else if (*x == 3) {
curr_state = check_sum(get_ascii_value(board[1][*y]) + get_ascii_value(board[2][*y]), player);
if (curr_state == YES && *y == 3) {
curr_state = check_sum(get_ascii_value(board[1][1]) + get_ascii_value(board[2][2]), player);
if (curr_state == YES && *y == 1) {
curr_state = check_sum(get_ascii_value(board[2][2]) + get_ascii_value(board[1][3]), player);
} else if (curr_state == YES && *y == 3) {
curr_state = check_sum(get_ascii_value(board[2][2]) + get_ascii_value(board[1][1]), player);
}
}
}

if (curr_state != YES)
return curr_state;

if (*y == 1) {
curr_state = check_sum(get_ascii_value(board[*x][2]) + get_ascii_value(board[*x][3]), player);
} else if (*y == 2) {
curr_state = check_sum(get_ascii_value(board[*x][1]) + get_ascii_value(board[*x][3]), player);
} else if (*y == 3) {
curr_state = check_sum(get_ascii_value(board[*x][1]) + get_ascii_value(board[*x][2]), player);
}

if (curr_state != YES)
return curr_state;

return YES;
}

// fills the given slot with the given player value
void fill_slot(unsigned short *x, unsigned short *y, string player) {
board[*x][*y] = "[" + player + "]";
}

// checks if the board is full
bool is_board_full() {
for (int i = 1; i < 4; i++) {
for (int j = 1; j < 4; j++) {
if (get_ascii_value(board[i][j]) == EMPTY_SUM)
return false;
}
}
return true;
}

// exits the game when the user press enter
void exit_game() {
cin.ignore();
cout << "Press enter to exit..." << endl;
cin.get();
exit(EXIT_SUCCESS);
}

// handles the current turn actions
void handle_turn(string player) {
unsigned short x, y;
bool error;
Action curr_state;

do {
error = false;
cout << "Player [" << player << "] enter (x) position (0..2): ";
cin >> x;
cout << "Player [" << player << "] enter (y) position (0..2): ";
cin >> y;

x = x + 1;
y = y + 1;

curr_state = get_action_from_movement(&x, &y, player);

if (curr_state == NO) {
error = true;
cout << "ERROR: Enter a valid action." << endl;
continue;
}

fill_slot(&x, &y, player);
print_board();

if (curr_state == X_WIN || curr_state == O_WIN) {
cout << "Player [" << player << "] wins!" << endl;
exit_game();
}

} while (error);

if (is_board_full()) {
cout << "The game has ended, the board is full." << endl;
exit_game();
}
}

int main() {
print_board();
while (running) {
handle_turn(X_PLAYER);
handle_turn(O_PLAYER);
}

return EXIT_SUCCESS;
}


Related Solutions

Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
In a game of tic tac toe. How do you check if all the positions in...
In a game of tic tac toe. How do you check if all the positions in a double array are taken, the double arrays are used to store the x's and o's?
Game of Tic Tac Toe with the following conditions A point system where a  move that leads...
Game of Tic Tac Toe with the following conditions A point system where a  move that leads to a winning game is given 1 point, a move that leads to a tie is given 0 point, and a  lead to a losing game will get -1 point. Grid size of 5x5 A timer that can be set for how long a game can be played 5 symbols in a row to get a point Connected lines cannot be crossed (No diagonal lines)...
please create a tic tac toe game with python using only graphics.py library
please create a tic tac toe game with python using only graphics.py library
If you were to write a game of tic-tac-toe, you may store the representation of the...
If you were to write a game of tic-tac-toe, you may store the representation of the game board as a two dimensional list such as   [['X', '', 'X'], ['O', 'X', ''], ['', 'O', 'X']] where each sublist is a row in the board.   Empty strings ('') denote a space that does not yet have a value. Assuming this representation, write functions (contained in the same file) that do the following: a) Create a new empty tic-tac-toe board. This function should...
If anyone can please write a code for a 5x5 tic tac toe game in matlab...
If anyone can please write a code for a 5x5 tic tac toe game in matlab I would greatly appreciate it. Its extra credit. PLEASE HELP ME :(
Provided is code for checking horizontal and vertical win in a tic tac toe game. Please...
Provided is code for checking horizontal and vertical win in a tic tac toe game. Please provide code for checking diagonal win. function checkWinVertical(player, row, col) { var counter = 0; //each button var btn = document.getElementsByTagName("button"); for (counterC = 0; counterC < col; counterC++){ for (countR = 0; countR < row; countR++){ if (player != btn[counter].innerHTML){ counter += 1; break; } else if (countR + 1 == col) { for (countW = 0; countW < col; countW++){ btn[counter -...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT