how to randomly generate moves until the whole board is filled?. If the board size is c*r, you can only call the random function rand() c*r times (excluding the random function rand() that have been used in the provided code). Below is the code I need to convert. It is manual right now (user inputs numbers until whole board filled).
I also have a piece of algorithmn below..but am unsure of how I can use it in the code:
//Algorithm
vector emptyCell;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (board.getGrid(i, j) == ' ')
emptyCell.push_back(i * c + j);
}
}
int index = rand() % emptyCell.size();
x = emptyCell[index] / r;
y = emptyCell[index] % c;
return;
//
//
//
//
//
//
//Code
#ifndef BOARD_H_
#define BOARD_H_
#include
#include
#include
#include
using namespace std;
// struct to record a user move (row and column value)
struct Move
{
int row,col;
};
class Board {
private:
int row, col;
int **grid; // 1 = black; -1 = white; 0 = empty
list left; //holds list of intergers
public:
Board(int r, int c) {
row = r;
col = c;
grid = new int*[row]; //placing a number
for (int i = 0; i < row; i++)
grid[i] = new int[col];
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
grid[i][j] = 0;
for (int i = 0; i < row*col; i++) {
left.push_back(i); //adds a new element at end of vector
}
}
//clear board
~Board() {
for (int i = 0; i < row; i++)
delete[] grid[i];
delete[] grid;
}
Board(Board& cboard) {
row = cboard.row;
col = cboard.col;
grid = new int*[row]; //placing numbers in grid
for (int i = 0; i < row; i++)
grid[i] = new int[col];
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
grid[i][j] = cboard.grid[i][j];
}
bool addMove(int player, int x, int y);
void checkNeighbors(int, int);
void printBoard();
void play();
};
void Board::checkNeighbors(int x, int y) {
return;
}
bool Board::addMove(int player, int x, int y) {
checkNeighbors(x, y);
grid[x][y] = rand()%6 + 1;
return true;
}
void Board::play() {
int x, y;
stack userMoves; //create a stack to record all user moves
do {
cout << "Game board:" << endl;
this->printBoard();
cout << "Input your move with row and column numbers:" << endl;
cin >> x >> y;
// record the move in the stack
Move move ;
move.row = x;
move.col = y;
userMoves.push(move);
addMove(1, x-1, y-1);
} while (true);
// print the stack contents
cout<<"User Moves : "<
while(!userMoves.empty())
{
cout<
userMoves.pop();
}
}
void Board::printBoard() {
cout << " ";
for (int j = 0; j < col; j++) {
cout << j + 1 << " ";
}
cout << endl;
for (int i = 0; i < row; i++) {
cout << " ";
for (int j = 0; j < col; j++) {
cout << " ---";
}
cout << endl;
cout << (i + 1) << " |";
for (int j = 0; j < col; j++) {
if (grid[i][j] == 0) {
cout << " |";
} else if (grid[i][j] > 0) {
cout << " " << grid[i][j] << " |";
} else if (grid[i][j] < 0) {
cout << "" << grid[i][j] << " |";
}
}
cout << endl;
}
cout << " ";
for (int j = 0; j < col; j++) {
cout << " ---";
}
cout << endl << endl;
}
#endif /* BOARD_H_ */
#include<bits/stdc++.h>
using namespace std;
#define BEGINNER 0
#define INTERMEDIATE 1
#define ADVANCED 2
#define MAXSIDE 25
#define MAXMINES 99
#define MOVESIZE 526 // (25 * 25 - 99)
int SIDE ; // side length of the board
int MINES ; // number of mines on the board
// A Utility Function to check whether given cell (row,
col)
// is a valid cell or not
bool isValid(int row, int col)
{
// Returns true if row number and column number
// is in range
return (row >= 0) && (row < SIDE)
&&
(col >= 0) && (col <
SIDE);
}
// A Utility Function to check whether given cell (row,
col)
// has a mine or not.
bool isMine (int row, int col, char board[][MAXSIDE])
{
if (board[row][col] == '*')
return (true);
else
return (false);
}
// A Function to get the user's move
void makeMove(int *x, int *y)
{
// Take the input move
printf("Enter your move, (row, column) -> ");
scanf("%d %d", x, y);
return;
}
// A Function to print the current gameplay board
void printBoard(char myBoard[][MAXSIDE])
{
int i, j;
printf (" ");
for (i=0; i<SIDE; i++)
printf ("%d ", i);
printf ("\n\n");
for (i=0; i<SIDE; i++)
{
printf ("%d ", i);
for (j=0; j<SIDE; j++)
printf ("%c ",
myBoard[i][j]);
printf ("\n");
}
return;
}
// A Function to count the number of
// mines in the adjacent cells
int countAdjacentMines(int row, int col, int mines[][2],
char
realBoard[][MAXSIDE])
{
int i;
int count = 0;
/*
Count all the mines in the 8
adjacent
cells
N.W N
N.E
\ | /
\ | /
W----Cell----E
/ | \
/ | \
S.W S S.E
Cell-->Current Cell (row,
col)
N --> North (row-1,
col)
S --> South (row+1,
col)
E --> East
(row, col+1)
W --> West
(row, col-1)
N.E--> North-East (row-1,
col+1)
N.W--> North-West (row-1,
col-1)
S.E--> South-East (row+1,
col+1)
S.W--> South-West (row+1,
col-1)
*/
//----------- 1st Neighbour (North) ------------
// Only process this cell if
this is a valid one
if (isValid (row-1, col) ==
true)
{
if (isMine
(row-1, col, realBoard) == true)
count++;
}
//----------- 2nd Neighbour (South) ------------
// Only process this cell if
this is a valid one
if (isValid (row+1, col) ==
true)
{
if (isMine
(row+1, col, realBoard) == true)
count++;
}
//----------- 3rd Neighbour (East) ------------
// Only process this cell if
this is a valid one
if (isValid (row, col+1) ==
true)
{
if (isMine (row,
col+1, realBoard) == true)
count++;
}
//----------- 4th Neighbour (West) ------------
// Only process this cell if
this is a valid one
if (isValid (row, col-1) ==
true)
{
if (isMine (row,
col-1, realBoard) == true)
count++;
}
//----------- 5th Neighbour (North-East) ------------
// Only process this cell if
this is a valid one
if (isValid (row-1, col+1) ==
true)
{
if (isMine
(row-1, col+1, realBoard) == true)
count++;
}
//----------- 6th Neighbour (North-West) ------------
// Only process this cell if
this is a valid one
if (isValid (row-1, col-1) ==
true)
{
if (isMine
(row-1, col-1, realBoard) == true)
count++;
}
//----------- 7th Neighbour (South-East) ------------
// Only process this cell if
this is a valid one
if (isValid (row+1, col+1) ==
true)
{
if (isMine
(row+1, col+1, realBoard) == true)
count++;
}
//----------- 8th Neighbour (South-West) ------------
// Only process this cell if
this is a valid one
if (isValid (row+1, col-1) ==
true)
{
if (isMine
(row+1, col-1, realBoard) == true)
count++;
}
return (count);
}
// A Recursive Fucntion to play the Minesweeper Game
bool playMinesweeperUtil(char myBoard[][MAXSIDE], char
realBoard[][MAXSIDE],
int mines[][2],
int row, int col, int *movesLeft)
{
// Base Case of Recursion
if (myBoard[row][col] != '-')
return (false);
int i, j;
// You opened a mine
// You are going to lose
if (realBoard[row][col] == '*')
{
myBoard[row][col]='*';
for (i=0; i<MINES; i++)
myBoard[mines[i][0]][mines[i][1]]='*';
printBoard (myBoard);
printf ("\nYou lost!\n");
return (true) ;
}
else
{
// Calculate the number of adjacent
mines and put it
// on the board
int count = countAdjacentMines(row,
col, mines, realBoard);
(*movesLeft)--;
myBoard[row][col] = count + '0';
if (!count)
{
/*
Recur for all 8
adjacent cells
N.W N N.E
\ | /
\ | /
W----Cell----E
/ | \
/ | \
S.W S S.E
Cell-->Current Cell (row, col)
N -->
North (row-1, col)
S -->
South (row+1, col)
E -->
East (row, col+1)
W -->
West (row, col-1)
N.E-->
North-East (row-1, col+1)
N.W-->
North-West (row-1, col-1)
S.E-->
South-East (row+1, col+1)
S.W-->
South-West (row+1, col-1)
*/
//----------- 1st Neighbour (North) ------------
// Only
process this cell if this is a valid one
if (isValid
(row-1, col) == true)
{
if (isMine (row-1, col, realBoard) ==
false)
playMinesweeperUtil(myBoard, realBoard, mines,
row-1, col, movesLeft);
}
//----------- 2nd Neighbour (South) ------------
// Only
process this cell if this is a valid one
if (isValid
(row+1, col) == true)
{
if (isMine (row+1, col, realBoard) ==
false)
playMinesweeperUtil(myBoard,
realBoard, mines, row+1, col, movesLeft);
}
//----------- 3rd Neighbour (East) ------------
// Only
process this cell if this is a valid one
if (isValid
(row, col+1) == true)
{
if (isMine (row, col+1, realBoard) ==
false)
playMinesweeperUtil(myBoard,
realBoard, mines, row, col+1, movesLeft);
}
//----------- 4th Neighbour (West) ------------
// Only
process this cell if this is a valid one
if (isValid
(row, col-1) == true)
{
if (isMine (row, col-1, realBoard) ==
false)
playMinesweeperUtil(myBoard,
realBoard, mines, row, col-1, movesLeft);
}
//----------- 5th Neighbour (North-East) ------------
// Only
process this cell if this is a valid one
if (isValid
(row-1, col+1) == true)
{
if (isMine (row-1, col+1, realBoard) ==
false)
playMinesweeperUtil(myBoard,
realBoard, mines, row-1, col+1, movesLeft);
}
//----------- 6th Neighbour (North-West) ------------
// Only
process this cell if this is a valid one
if (isValid
(row-1, col-1) == true)
{
if (isMine (row-1, col-1, realBoard) ==
false)
playMinesweeperUtil(myBoard,
realBoard, mines, row-1, col-1, movesLeft);
}
//----------- 7th Neighbour (South-East) ------------
// Only
process this cell if this is a valid one
if (isValid
(row+1, col+1) == true)
{
if (isMine (row+1, col+1, realBoard) ==
false)
playMinesweeperUtil(myBoard,
realBoard, mines, row+1, col+1, movesLeft);
}
//----------- 8th Neighbour (South-West) ------------
// Only
process this cell if this is a valid one
if (isValid
(row+1, col-1) == true)
{
if (isMine (row+1, col-1, realBoard) ==
false)
playMinesweeperUtil(myBoard,
realBoard, mines, row+1, col-1, movesLeft);
}
}
return (false);
}
}
// A Function to place the mines randomly
// on the board
void placeMines(int mines[][2], char realBoard[][MAXSIDE])
{
bool mark[MAXSIDE*MAXSIDE];
memset (mark, false, sizeof (mark));
// Continue until all random mines have been
created.
for (int i=0; i<MINES; )
{
int random = rand() %
(SIDE*SIDE);
int x = random / SIDE;
int y = random % SIDE;
// Add the mine if no mine is
placed at this
// position on the board
if (mark[random] == false)
{
// Row Index of
the Mine
mines[i][0]=
x;
// Column Index
of the Mine
mines[i][1] =
y;
// Place the
mine
realBoard[mines[i][0]][mines[i][1]] = '*';
mark[random] =
true;
i++;
}
}
return;
}
// A Function to initialise the game
void initialise(char realBoard[][MAXSIDE], char
myBoard[][MAXSIDE])
{
// Initiate the random number generator so that
// the same configuration doesn't arises
srand(time (NULL));
// Assign all the cells as mine-free
for (int i=0; i<SIDE; i++)
{
for (int j=0; j<SIDE; j++)
{
myBoard[i][j] =
realBoard[i][j] = '-';
}
}
return;
}
// A Function to cheat by revealing where the mines are
// placed.
void cheatMinesweeper (char realBoard[][MAXSIDE])
{
printf ("The mines locations are-\n");
printBoard (realBoard);
return;
}
// A function to replace the mine from (row, col) and put
// it to a vacant space
void replaceMine (int row, int col, char board[][MAXSIDE])
{
for (int i=0; i<SIDE; i++)
{
for (int j=0; j<SIDE; j++)
{
// Find the first location in the board
// which is not having a mine and put a
mine
// there.
if (board[i][j] != '*')
{
board[i][j] = '*';
board[row][col] = '-';
return;
}
}
}
return;
}
// A Function to play Minesweeper game
void playMinesweeper ()
{
// Initially the game is not over
bool gameOver = false;
// Actual Board and My Board
char realBoard[MAXSIDE][MAXSIDE],
myBoard[MAXSIDE][MAXSIDE];
int movesLeft = SIDE * SIDE - MINES, x, y;
int mines[MAXMINES][2]; // stores (x,y) coordinates of
all mines.
initialise (realBoard, myBoard);
// Place the Mines randomly
placeMines (mines, realBoard);
/*
If you want to cheat and know
where mines are before playing the game
then uncomment this part
cheatMinesweeper(realBoard);
*/
// You are in the game until you have not opened a
mine
// So keep playing
int currentMoveIndex = 0;
while (gameOver == false)
{
printf ("Current Status of Board :
\n");
printBoard (myBoard);
makeMove (&x, &y);
// This is to guarantee that the
first move is
// always safe
// If it is the first move of the
game
if (currentMoveIndex == 0)
{
// If the first
move itself is a mine
// then we
remove the mine from that location
if (isMine (x,
y, realBoard) == true)
replaceMine (x, y, realBoard);
}
currentMoveIndex ++;
gameOver = playMinesweeperUtil (myBoard, realBoard, mines, x, y, &movesLeft);
if ((gameOver == false)
&& (movesLeft == 0))
{
printf ("\nYou
won !\n");
gameOver =
true;
}
}
return;
}
// A Function to choose the difficulty level
// of the game
void chooseDifficultyLevel ()
{
/*
--> BEGINNER = 9 * 9 Cells and 10 Mines
--> INTERMEDIATE = 16 * 16 Cells and 40 Mines
--> ADVANCED = 24 * 24 Cells and 99 Mines
*/
int level;
printf ("Enter the Difficulty Level\n");
printf ("Press 0 for BEGINNER (9 * 9 Cells and 10
Mines)\n");
printf ("Press 1 for INTERMEDIATE (16 * 16 Cells and
40 Mines)\n");
printf ("Press 2 for ADVANCED (24 * 24 Cells and 99
Mines)\n");
scanf ("%d", &level);
if (level == BEGINNER)
{
SIDE = 9;
MINES = 10;
}
if (level == INTERMEDIATE)
{
SIDE = 16;
MINES = 40;
}
if (level == ADVANCED)
{
SIDE = 24;
MINES = 99;
}
return;
}
// Driver Program to test above functions
int main()
{
/* Choose a level between
--> BEGINNER = 9 * 9 Cells and 10 Mines
--> INTERMEDIATE = 16 * 16 Cells and 40 Mines
--> ADVANCED = 24 * 24 Cells and 99 Mines
*/
chooseDifficultyLevel ();
playMinesweeper ();
return (0);
}
how to randomly generate moves until the whole board is filled?. If the board size is...
There is a problem with thecode. I get the error messages " 'board' was not declared in this scope", \src\TicTacToe.cpp|7|error: expected unqualified-id before ')' token| \src\TicTacToe.cpp|100|error: expected declaration before '}' token| Here is the code #ifndef TICTACTOE_H #define TICTACTOE_H #include class TicTacToe { private: char board [3][3]; public: TicTacToe () {} void printBoard(); void computerTurn(char ch); bool playerTurn (char ch); char winVerify(); }; ************************************ TicTacToe.cpp #endif // TICTACTOE_H #include "TicTacToe.h" #include #include using namespace std; TicTacToe() { for(int i =...
You've been hired by Rugged Robots to complete a C++ console application that controls a robotic floor vacuum. Wayne State software engineers have already developed the following three files: File Description Lab20-01-Key.cpp This is the user application. RuggedRobotLibrary.h This is the library header file. It contains constant declarations and function prototypes. It is included by the other two files. RuggedRobotLibrary.cpp This is the library file. It contains functions that may be used in any user application. This is considered an...
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...
Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...
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)<<"...
MOdify the program below to do 3x3 matrix mutiplications: #include <iostream> #include <cstdlib> #include <pthread.h> using namespace std; int A[3] [3] = {{1,2},{3,4}}; int B[3] [3] = {{5,6},{7,8}}; int C[3] [3]; struct Position{ int row; int col; }; void *CalculateElement(void *pos){ Position *p = (Position *)pos; C[p->row][p->col] = 0; for(int i = 0; i < 3; i++){ C[p->row][p->col] += A[p->row][i]*B[i][p->col]; } pthread_exit(NULL); } const int NUM_THREADS = 4; int main() { ...
This program is in C++ which reserves flight seats. It uses a file imported to get the seat chart called "chartIn.txt" which looks like this 1 A B C D E F 2 A B C D E F It continues until row 10. Sorry unable to provide the chartIn.txt file. I am having issues with function displaySeats, it displays then but when it comes to 10 the row looks like this 1 0 A B C D E ,...
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;...
I need help with the following and written in c++ thank you!: 1) replace the 2D arrays with vectors 2) add a constructor to the Sudoku class that reads the initial configuration from a file 3) adds a function to the Sudoku class that writes the final Sudoku grid to a file or to the standard output device, cout. Sudoku.h #pragma once /* notes sudoku() default constructor precondition : none postcondition: grid is initialized to 0 sudoku(g[][9]) 1-parameter constructor precondition...
need help..... sudoku.h class sudoku { public: sudoku(); //default constructor //Postcondition: grid is initialized to 0 sudoku(int g[][9]); //constructor //Postcondition: grid = g void initializeSudokuGrid(); //Function to prompt the user to specify the numbers of the //partially filled grid. //Postcondition: grid is initialized to the numbers // specified by the user. void initializeSudokuGrid(int g[][9]); //Function to initialize grid to g //Postcondition: grid = g; void printSudokuGrid(); //Function to print the sudoku grid. bool solveSudoku(); //Funtion to solve the sudoku problem....