In C++
There is modification to the original assignment - mainly the input/output.
Instead of having the players enter the coordinates , the players will enter a number (1 - 9) instead.
The game board should be displayed as:
1 2 3
4 5 6
7 8 9
If player O enters a 5, the game board will become:
1 2 3
4 O 6
7 8 9
=================================================================================
If a player entered a character other than 1 - 9, the program will indicate the fact and let the same player try again.
If a player entered a position (1 - 9) that is already occupied, the program will indicate the fact and let the same player try again.
As soon as a winner emerges, the program announces the winner and terminate the game.
If all positions are taken and no winner, the program announces the game result as a tie.
*** Note 1: please avoid hard coding, think in the future you might expand the game from 3 x 3 to m x m.
*** Note 2: to convert between int and string:
int i = stoi("8");
string s = to_string(100);
*** Note 3:
0 1 2 -----------j
i----- 0 1 2 3
1 4 5 6
2 7 8 9
here i is index for row, j is index for column
if given value n (1- 9),
i = (n-1) / 3
j = (n-1) % 3
if given i and j,
n = 3 * i + j + 1
below steps can be follwoed to complete the problem :
more info and code :
code :
#include <iostream>
#include <string>
using namespace std;
const int row = 3;
const int col = 3;
const string player_id[] = {"O" , "X"};
// function to place the player's marker at specified
number in the grid.
void modify_grid(string grid[row][col] , int num , int
player_index){
int i = (num-1) / row;
int j = (num-1) % col;
if(grid[i][j] == player_id[player_index] && grid[i][j] ==
player_id[player_index]){
cout<<"This cell is already occupied\n";
}
// cout<<i<<" "<<j;
grid[i][j] = player_id[player_index];
}
// function to print the grid.
void print_grid(string grid[row][col]){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
cout<<grid[i][j]<<" ";
}
cout<<endl;
}
}
// function to check if current player has won the
game.
bool check_winner(string grid[row][col] , int player_index){
// travers the grid for all rows , all columns , diagonal from
top_left to bottom right , diagonal from top right to
bottom_left
// and see we have a full set of current player marker, return true
if found , else false.
for(int i=0;i<row;i++){
int consecutive_count = 0;
for(int j=0;j<col;j++){
if (grid[i][j] == player_id[player_index]){
consecutive_count += 1;
}
}
if(consecutive_count == col){
return true;
}
}
for(int i=0;i<col;i++){
int consecutive_count = 0;
for(int j=0;j<row;j++){
if (grid[i][j] == player_id[player_index]){
consecutive_count += 1;
}
}
if(consecutive_count == col){
return true;
}
}
int i = 0;
int j = 0;
int consecutive_count_diag_left = 0;
while (i<row && j < col){
if (grid[i][j] == player_id[player_index]){
consecutive_count_diag_left += 1;
}
i += 1;
j += 1;
}
if(consecutive_count_diag_left == row){
return true;
}
i = 0;
j = col-1;
int consecutive_count_diag_right = 0;
while (i<row && j >= 0){
if (grid[i][j] == player_id[player_index]){
consecutive_count_diag_right += 1;
}
i += 1;
j -= 1;
}
if(consecutive_count_diag_right == row){
return true;
}
return false;
}
bool is_full(string grid[row][col]){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(grid[i][j] != "O" && grid[i][j] != "X"){
return false;
}
}
}
return true;
}
int main(){
string grid[row][col];
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
int value = (3*i)+j+1;
grid[i][j] = to_string(value);
}
}
print_grid(grid);
cout<<"Player 0 has 'O' marker\n Player 1 has 'X'
marker\n";
cout<<"Lets start the game\n";
int current_player = 1;
int num = -1;
while (!is_full(grid) && !check_winner(grid ,
current_player)){
current_player = 1 - current_player;
cout<<"Player "<<current_player<<" Enter the
integer to be replaced: ";
cin>>num;
if(num < 1 && num > (row*col)){
cout<<"Invalid input\n";
continue;
}
modify_grid(grid , num , current_player);
print_grid(grid);
}
if(check_winner(grid , current_player)){
cout<<"Player "<<current_player<<" Won the
game!!\n";
}
else{
cout<<"GamE Tied\n";
}
return 0;
}
In C++ There is modification to the original assignment - mainly the input/output. Instead of having...
JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 The program will assign X to Player 1, and O to Player The program will ask Player 1, to...
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;...
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...
Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...
C programming language Purpose The purpose of this assignment is to help you to practice working with functions, arrays, and design simple algorithms Learning Outcomes ● Develop skills with multidimensional arrays ● Learn how to traverse multidimensional arrays ● Passing arrays to functions ● Develop algorithm design skills (e.g. recursion) Problem Overview Problem Overview Tic-Tac-Toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the...
How to I change this code to print the highest value(s) in the
array? The wanted output (you just need to make it print the
winner, ignore what comes before) is included below:
--------------------------------------------------------------------------------------------
public static void playGame(int players, int cards) {
if (players * cards > 52) {
System.out.println("Not enough cards for that many
players.");
return;
}
boolean[] deck = new boolean[52];
int[] playerScore = new int[players];
for (int i = 0; i < players; i++) {
System.out.println("Player "...
I have already finished most of this assignment. I just need some help with the canMove and main methods; pseudocode is also acceptable. Also, the programming language is java. Crickets and Grasshoppers is a simple two player game played on a strip of n spaces. Each space can be empty or have a piece. The first player plays as the cricket pieces and the other plays as grasshoppers and the turns alternate. We’ll represent crickets as C and grasshoppers as...
This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe { private char currentPlayer; private char[][] board; private char winner; /** * Default constructor * Initializes the board to be 3 by 3 and...
Use Java language to create this program Write a program that allows two players to play a game of tic-tac-toe. Using a two-dimensional array with three rows and three columns as the game board. Each element of the array should be initialized with a number from 1 - 9 (like below): 1 2 3 4 5 6 7 8 9 The program should run a loop that Displays the contents of the board array allows player 1 to select the...
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...