Question

This is an advanced version of the game tic tac toe, where the player has to...

This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20.

Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array.

Use this starter code to complete the assignment in C++. Write code at the comments that start with //YOU:

#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <unistd.h>
#include "/public/colors.h"
using namespace std;

const char EMPTY = '.';
const char PLAYER1 = 'X', PLAYER2 = 'O', WALL = '#';
const int ROWS = 20, COLS = 30, WIN = 5;

void print_array(char arr[ROWS][COLS]) {
for (int i = 0; i < COLS + 2; i++)
cout << WALL;
cout << endl;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (j == 0) cout << WALL; //Right wall
cout << arr[i][j];
if (j == COLS - 1) cout << WALL; //Right wall
}
cout << endl;
}
for (int i = 0; i < COLS + 2; i++)
cout << WALL;
cout << endl;
}

void refresh(char arr[ROWS][COLS]) {
system("clear");
print_array(arr);
}

bool check_win(char arr[ROWS][COLS], char turn) {
for (int i = 0; i < ROWS; i++) {
int in_a_row = 0;
for (int j = 0; j < COLS; j++) {
if (arr[i][j] == turn) in_a_row++;
else in_a_row = 0;
if (in_a_row >= WIN) return true;
}
}
return false;
}

//YOU: Check five in a row vertically (easy, just flip rows and columns)

//YOU: Check five in a row diagonally (medium)

//YOU: Check five in a row backwards diagonally (hard)

int main() {
int players = 0;
while (players < 1 or players > 2) {
cout << "Do you want single player (1) or two player (2) mode?\n";
cin >> players;
if (!cin) exit(1);
}
int seed = 0;
if (players == 1) {
cout << "Please enter the seed for the random number generator.\n";
cin >> seed;
if (!cin) exit(1);
}

char arr[ROWS][COLS];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
arr[i][j] = EMPTY;
}
}
//Human is always player 1 ('X')
//AI is always player 2 ('O')
bool first_player = true;
int moves = 0;
while (true) {
int x = 0, y = 0;
char turn = first_player ? PLAYER1 : PLAYER2;
if (first_player or players == 2) {
cout << "Please enter x,y location:\n";
cin >> x >> y;
if (!cin) exit(1);
if (arr[y][x] != EMPTY) continue;

//YOU: Check for the spot being in bounds

arr[y][x] = turn;
} else {

//YOU: Generate a random column and row using rand() modulus the row / column size
/YOU: If that spot is taken, continue
//YOU: Else, mark that spot with an O

}
refresh(arr);
if (check_win(arr, turn)) {
cout << "Player " << turn << " wins!\n";
exit(0);
}
//YOU: Write code to check for a draw here
if (false) {
cout << "Game is a draw.\n";
exit(0);
}
first_player = !first_player;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <unistd.h>
//#include "/public/colors.h"
using namespace std;

const char EMPTY = '.';
const char PLAYER1 = 'X', PLAYER2 = 'O', WALL = '#';
const int ROWS = 20, COLS = 30, WIN = 5;

void print_array(char arr[ROWS][COLS]) {
   for (int i = 0; i < COLS + 2; i++)
       cout << WALL;
   cout << endl;
      
   for (int i = 0; i < ROWS; i++) {
       for (int j = 0; j < COLS; j++) {
           if (j == 0) cout << WALL; //Right wall
           cout << arr[i][j];
           if (j == COLS - 1) cout << WALL; //Right wall
       }
       cout << endl;
   }
  
   for (int i = 0; i < COLS + 2; i++)
       cout << WALL;
   cout << endl;
}

void refresh(char arr[ROWS][COLS]) {
   system("cls");
   print_array(arr);
}

bool isFull(char arr[ROWS][COLS]){
   for (int i = 0; i < ROWS; i++) {
       for (int j = 0; j < COLS; j++) {
           if (arr[i][j]) return false;
       }
   }
   return true;
}


bool check_win_h(char arr[ROWS][COLS], char turn) {
   for (int i = 0; i < ROWS; i++) {
       int in_a_row = 0;
       for (int j = 0; j < COLS; j++) {
           if (arr[i][j] == turn) in_a_row++;
           else in_a_row = 0;
           if (in_a_row >= WIN) return true;
       }
   }
   return false;
}

bool check_win_v(char arr[ROWS][COLS], char turn) {
   for (int i = 0; i < COLS; i++) {
       int in_a_col = 0;
       for (int j = 0; j < ROWS; j++) {
           if (arr[i][j] == turn) in_a_col++;
           else in_a_col = 0;
           if (in_a_col >= WIN) return true;
       }
   }
   return false;
}


bool check_win_d(char arr[ROWS][COLS], char turn) {
   for (int i = 0; i < ROWS-WIN; i++) {
       for (int j = 0; j < COLS-WIN; j++) {
           int in_a_d = 0;
          
           int a=i,b=j;      
           for(int k=0;k<5;k++){
               if (arr[a+k][b+k] == turn) in_a_d++;
               else in_a_d = 0;
           }
          
           if (in_a_d >= WIN) return true;
       }
   }
   return false;
}


bool check_win_bd(char arr[ROWS][COLS], char turn) {
   for (int i = ROWS-WIN-1; i >=0 ; i--) {
       for (int j = COLS-WIN-1; j >=0 ; j--) {
           int in_a_bd = 0;
          
           int a=i,b=j;      
           for(int k=0;k<5;k++){
               if (arr[a+k][b-k] == turn) in_a_bd++;
               else in_a_bd = 0;
           }
          
           if (in_a_bd >= WIN) return true;
       }
   }
   return false;
}

int main() {
   int players = 0;
  
   while (players < 1 or players > 2) {
       cout << "Do you want single player (1) or two player (2) mode?\n";
       cin >> players;
       if (!cin) exit(1);
   }
  
   int seed = 0;
   if (players == 1) {
       cout << "Please enter the seed for the random number generator.\n";
       cin >> seed;
       if (!cin) exit(1);
   }
  
   srand(seed);
              
              
   char arr[ROWS][COLS];
   for (int i = 0; i < ROWS; i++) {
       for (int j = 0; j < COLS; j++) {
           arr[i][j] = EMPTY;
       }
   }
  
   //Human is always player 1 ('X')
   //AI is always player 2 ('O')
  
   bool first_player = true;
   int moves = 0;
  
   while (true) {
       int x = 0, y = 0;
       char turn = first_player ? PLAYER1 : PLAYER2;
      
       if (first_player or players == 2) {
           cout << "Please enter x,y location:\n";
           cin >> x >> y;
          
           if (!cin) exit(1);
           if(!(y>=0 && y<ROWS) || !(x>=0 && x<COLS)) {
               cout<<"x or y out of range\n";continue;  
           }
          
           if (arr[y][x] != EMPTY){
               cout<<"Already occupied\n";continue;      
           }
          
           arr[y][x] = turn;
       } else {
          
           while(true){
               x=rand()%ROWS;
               y=rand()%COLS;
              
               if (arr[x][y] == EMPTY){
                   arr[x][y] = turn;
                   break;
               }
           }
       }
       refresh(arr);
      
      
      
       if (check_win_h(arr, turn) || check_win_v(arr, turn) || check_win_d(arr, turn) || check_win_bd(arr, turn)) {
           cout << "Player " << turn << " wins!\n";
           exit(0);
       }

       if (isFull(arr)) {
           cout << "Game is a draw.\n";
           exit(0);
       }
       first_player = !first_player;
   }
  
   system("pause");
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
This is an advanced version of the game tic tac toe, where the player has to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use...

    Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter...

  • In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and...

    In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and crosses on the 3 x 3 game board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row in the game board wins the game. Super Tic Tac Toe game also has a 3 x 3 game board with super grid. Each super grid itself is a traditional Tic Tac Toe board. Winning a game of Tic...

  • I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<i...

    I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...

  • Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std;...

    Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){    int rows = 5; int cols = 5; int x;    int** arr = new int[rows][cols]    cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x];    return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){    int rows; int cols; int x;...

  • 18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use di...

    18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...

  • TIC TAC TOE in openGL/ C++

    I just cannot figure out how to get the game to alternate turns. it is designed to be a 2 player game. the problem is once either an x or an o is placed it will onlycontinue to place the x or the o.please show me how to do the rest here is my code://#include <windows.h> // This header file will be needed for some windows compilers//#include <GL/gl.h> // gl.h and glu.h also maybe needed for some compilers//#include <GL/glu.h>#include <GL/glut.h>...

  • The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[...

    The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[SIZE];    int player, choice, win, count;    char mark;    count = 0; // number of boxes marked till now    initBoard(board);       // start the game    player = 1; // default player    mark = 'X'; // default mark    do {        displayBoard(board);        cout << "Player " << player << "(" << mark...

  • You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people....

    You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of his/her marks on the board in a row, column, or diagonal, he/she wins. When the board fills up with neither player winning, the game ends in a draw 1) Player 1 VS Player 2: Two players are playing the game...

  • Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you...

    Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you must copy and paste to cloud9. Do not change the provided complete functions, or the function stub headers / return values. Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT