In my Hitori game, the player removes numbers from the gameboard.
The game is won if all rows and columns have only different numbers.
In line 43, I would need to make some check function so that the board is checked after every removal, but I dont know how to do that. Should I perhaps make several for-loops?
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std; using Board = std::vector<vector<int>>;
const unsigned char EMPTY = ' ';
void initBoard(Board& board) {
string input = "1 1 3 1 2 2 2 2 3";
istringstream is { input };
for (auto& row : board)
{
for(auto& column : row)
{
is >> column;
}
}
}
void printBoard(const Board& board) {
for(unsigned int row = 0; row < 3; ++row)
{
cout << "| " << row + 1 << " | ";
for(unsigned int column = 0; column < 3; ++column)
{
if(board.at(row).at(column) == 0)
{
cout << EMPTY << " ";
}
else
{
cout << board.at(row).at(column) << " ";
}
}
cout << "|" << endl;
}
// CHECK IF EACH ROW AND COLUMN CONTAIN THE SAME NUMBER SEVERAL TIMES
// IF NOT -> THE GAME HAS BEEN WON
}
void updateBoard(Board& board) {
int x, y;
while (true) {
cout << "Enter coordinates (x, y): ";
cin >> x;
cin >> y;
board.at(y-1).at(x-1) = 0;
printBoard(board);
}
}
int main()
{
Board board(3, vector<int>(3)); // board is vector with 3 rows and 3 columns
initBoard(board);
printBoard(board);
updateBoard(board);
}