unbeatable Tic Tac Toe - c++

i'm trying to make a unbeatable tic tac toe for a side project and i can't make it right (i can actually beat it ironically).
It's actually a implementation of the MiniMax algorithm; i came with this code
#include <iostream>
#include <string>
using namespace std;
struct Move
{
int line, columns;
};
//Return the number of remainings turn based on the number of lest boxes
int remainingsTurns(char grid[3][3])
{
int remainingTurn = 0;
for (int k = 0; k < 3; k++)
{
for (int i = 0; i < 3; i++)
{
if (grid[k][i] == ' ')
{
remainingTurn++;
}
}
}
return remainingTurn;
}
//Print the grid on screen
void printGrid(char grid[3][3])
{
for (int k = 0; k < 3; k++)
{
for (int i = 0; i < 3; i++)
{
cout << "| " << grid[k][i] << " ";
}
cout << "|" << endl;
}
}
//Give a value to the board
int evaluateBoard(char grid[3][3])
{
//Check the board for lines
for (int k = 0; k < 3; k++)
{
if (grid[k][0] == grid[k][1] && grid[k][1] == grid[k][2])
{
if (grid[k][0] == 'x')
{
return +10;
}
else if (grid[k][0] == 'o')
{
return -10;
}
}
}
//Check the board for columns
for (int k = 0; k < 3; k++)
{
if (grid[0][k] == grid[1][k] && grid[1][k] == grid[2][k])
{
if (grid[0][k] == 'x')
{
return +10;
}
else if (grid[0][k] == 'o')
{
return -10;
}
}
}
//Check the board for diagonals
if (grid[0][0] == grid[1][1] && grid[0][0] == grid[2][2])
{
if (grid[0][0] == 'x')
{
return +10;
}
else if (grid[0][0] == 'o')
{
return -10;
}
}
if (grid[0][2] == grid[1][1] && grid[0][2] == grid[2][0])
{
if (grid[0][0] == 'x')
{
return +10;
}
else if (grid[0][0] == 'o')
{
return -10;
}
}
//if no ictory return 0
return 0;
}
// MiniMax algorithm
int miniMax(char grid[3][3], int turn, bool maxMove)
{
int score = evaluateBoard(grid);
if (score == 10)
{
return score;
}
if (score == -10)
{
return score;
}
//Check if the game is a tie
if (remainingsTurns(grid) == 0)
{
return 0;
}
if (maxMove)
{
int best = -1000;
for (int k = 0; k < 3; k++)
{
for (int i = 0; i < 3; i++)
{
if (grid[k][i] == ' ')
{
grid[k][i] = 'x';
best = max(best, miniMax(grid, turn + 1, !maxMove));
grid[k][i] = ' ';
}
}
}
return best;
}
else
{
int best = 1000;
for (int k = 0; k < 3; k++)
{
for (int i = 0; i < 3; i++)
{
if (grid[k][i] == ' ')
{
grid[k][i] = 'o';
best = min(best, miniMax(grid, turn + 1, !maxMove));
grid[k][i] = ' ';
}
}
}
return best;
}
}
Move playerMov(char grid[3][3])
{
Move playerMove;
int input = -1;
cout << "Enter the column you want to play (1, 2 or 3)" << endl;
cin >> input;
if (input == 1 || input == 2 || input == 3)
{
playerMove.columns = input-1;
input = -1;
}
else
{
cout << "Error, enter a valid number!" << endl;
playerMov(grid);
}
cout << "Enter the line you want to play (1, 2 or 3)" << endl;
cin >> input;
if (input == 1 || input == 2 || input == 3)
{
playerMove.line = input-1;
input = -1;
}
else
{
cout << "Error, enter a valid number!" << endl;
playerMov(grid);
}
return playerMove;
}
//return the best move using the MiniMax
Move findMove(char grid[3][3])
{
int best = -1000;
Move move;
move.line = -1;
move.columns = -1;
//Check all move to find if this move is the best possible move
for (int k = 0; k < 3; k++)
{
for (int i = 0; i < 3; i++)
{
if (grid[k][i] == ' ')
{
grid[k][i] = 'x';
int moveValue = miniMax(grid, 0, false);
grid[k][i] = ' ';
if (moveValue > best)
{
move.line = k;
move.columns = i;
}
}
}
}
return move;
}
int main()
{
char grid[3][3];
int turn = 0;
Move playerMove, algoMove;
for (int k = 0; k < 3; k++)
{
for (int i = 0; i < 3; i++)
{
grid[k][i] = ' ';
}
}
cout << "Welcome to the unbeatable Tic Tac Toe !" << endl;
do
{
printGrid(grid);
playerMove = playerMov(grid);
grid[playerMove.line][playerMove.columns] = 'o';
Move computerMove = findMove(grid);
grid[computerMove.line][computerMove.columns] = 'x';
} while (remainingsTurns(grid) > 0);
cout << endl;
}
but the movements of the algorithm doesn't seems right, it always choose the bottom right corner and i don't understand why...
This implementation is largely inspired by this article from Geek for Geek where i tried to steal the algorithm but i can't get it right to make it fit for single player.
What do i miss?

There are multiple bugs in your code:
You are checking the value of grid[0][0] in grid[0][2] == grid[1][1] && grid[0][2] == grid[2][0] case of the function evaluateBoard. It should be grid[0][2].
playerMov(grid); in the function playerMov should be return playerMov(grid);. Otherwise, the re-entered "correct" values will be dropped and (partly) uninitalized playerMov will be returned.
You should update best to moveValue when moveValue > best in the function findMove. (this should be the cause of the issue on your question)

Related

How do I make a function to check for the winning move in tic-tac-toe if the user chooses the size of the game board?

I have changed a Tic-Tac-Toe program from using a normal 3x3 grid to a grid-size chosen by the user (between 3 and 9). I am using a global constant 'SIZE' to hold the value the user chooses. My issue is adapting my winMove(); function so that it adjusts to check for the winning move based on the current board size chosen by the user(SIZE). I have tried different loops but can't get it to work. Right now it will only work with a 3x3 board.
I am still learning c++ and I have been stuck on this for a few days so I'm hoping a friendly person can help. This is my full program so far, hope it's not too much of a mess!
#include <iostream>
using namespace std;
struct TicTacToe
{
char **board;
};
void makeBoard(TicTacToe&);
void deallocBoard(TicTacToe&);
void printBoard(TicTacToe);
bool isDraw(TicTacToe);
void convertInput(char, char, int&, int&);
char winMove(TicTacToe, int, int);
bool validateSIZE(int);
int SIZE = 1;
int main(){
while(SIZE < 3 || SIZE > 9)
{
cout << "Enter number between 3 and 9 for the length of board.\n";
cout << "Example: 4 will make a board with 4 rows and 4 columns: ";
cin >> SIZE;
validateSIZE(SIZE);
}
TicTacToe game;
makeBoard(game);
char winner = 0;
char turn = 'X';
char rowIn, colIn;
int row, column;
while(!winner && !isDraw(game))
{
printBoard(game);
cout << "\nPlayer " << turn << "'s move (input format: a1): ";
cin >> rowIn >> colIn;
convertInput(rowIn, colIn, row, column);
if (game.board[row][column]==' ')
{
game.board[row][column] = turn;
if (turn == 'X')
{turn = 'O';}
else
{turn = 'X';}
winner = winMove(game, row, column);
}
else
cout << "Taken, try again!\n";
}
printBoard(game);
if (winner == 'X' || winner == 'O')
{cout << " Congrats, the winner is " << winner << '.' << endl;}
else
{cout << " Game ends in a draw." << endl;}
cout << endl << "Game Over!" << endl << endl;
deallocBoard(game);
return 0;
}
// Need help with this function.
char winMove(TicTacToe gameIn, int i, int j)
{
//row win
if (gameIn.board[i][0]==gameIn.board[i][1] &&
gameIn.board[i][0]==gameIn.board[i][2])
{
return gameIn.board[i][j];
}
//column win
if (gameIn.board[0][j]==gameIn.board[1][j] &&
gameIn.board[0][j]==gameIn.board[2][j])
{
return gameIn.board[i][j];
}
//left diagonal win
if (gameIn.board[0][0] != ' ' &&
gameIn.board[0][0] == gameIn.board[1][1] &&
gameIn.board[0][0] == gameIn.board[2][2])
{
return gameIn.board[i][j];
}
//right diagonal win
if (gameIn.board[0][2] != ' ' &&
gameIn.board[0][2] == gameIn.board[1][1] &&
gameIn.board[0][2] == gameIn.board[2][0])
{
return gameIn.board[i][j];
}
return 0;
}
bool validateSIZE(int SIZE)
{
if(SIZE < 3 || SIZE > 9)
{
cout << "\n\nNumber must be between 3 and 9!\nTry again!\nPlease ";
return false;
}
return true;
};
void makeBoard(TicTacToe& gameIn)
{
gameIn.board = new char*[SIZE];
for(int i = 0; i < SIZE; i++)
{gameIn.board[i] = new char[SIZE];}
for(int j =0; j < SIZE; j++)
for(int k = 0; k < SIZE; k++)
{gameIn.board[j][k] = ' ';}
}
void deallocBoard(TicTacToe& gameIn)
{
for(int i = 0; i < SIZE; i++)
delete [] gameIn.board[i];
delete [] gameIn.board;
gameIn.board = NULL;
}
void printBoard(TicTacToe gameIn)
{
int temp = 1;
cout << " ";
while(temp < SIZE + 1)
{
cout << temp << " ";
temp++;
}
temp = 1;
cout << endl;
for(int i = 0; i < SIZE; i++)
{
cout << char(i + 'a') << '|';
for(int j = 0; j < SIZE; j++)
{
cout << gameIn.board[i][j] << '|';
}
cout << endl;
}
}
bool isDraw(TicTacToe gameIn)
{
bool full = true;
for(int i = 0; full && i < SIZE; i++)
for(int j = 0; full && j < SIZE; j++)
full = gameIn.board[i][j] != ' ';
return full;
}
void convertInput(char rowIn, char colIn, int& row, int& column)
{
row = toupper(rowIn) - 'A';
column = colIn - '1';
}
You can easily use 2 for loops to iterate every starting point and its direction to preform victory checking. Something that would look like this:
// hor ver diagonals
// dx[4] = {1, 0, 1, 1};
// dy[4] = {0, 1, 1, -1};
// check horizontal win
for(int i = 0; i < SIZE; ++i)
{
bool win = true;
for(int j = 1; j < SIZE; ++j)
{
if(gameIn.board[j][i] != gameIn.board[j - 1][i])
{
win = false;
break;
}
}
if(win == true){return ;}
}
// check vertical win
.
.
.
// check diagonal 1 win
for(int i = 1; i < SIZE; ++i)
{
if(gameIn.board[i][i] != gameIn.board[i - 1][i - 1])
break;
if(i == SIZE - 1)return ;
}
// check diagonal 2 win
for(int i = 1; i < SIZE; ++i)
{
if(gameIn.board[i][SIZE - i - 1] != gameIn.board[i - 1][SIZE - i])
break;
if(i == SIZE - 1)return ;
}
I'll leave the vertical win for you.

I made the 2048 game but its not running as it was supposed to [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I decided to make the 2048 game. It is a very popular game for beginners.
The game is almost complete now but there's this error because of which my game keeps getting closed again and again as soon as I make my first move. I have tested a lot thoroughly and couldn't trace the error(s). I am new to programming.
#include<iostream>
#include<conio.h>
#include<ctime>
#include<string>
#include<cstdlib>
#include<fstream>
#include<Windows.h>
using namespace std;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
const int limit = 4;
int board[limit][limit] = {};
int score = 0;
string name;
int highestScore;
void newGame();
void displayBoard();
bool gameOver();
int generateNum(); //generate random 2 or 4
void moveInDirection(int);
void generateNumInBoard();
int main()
{
int currentDirection;
char command, choice;
char direction[128];
string currentName;
direction['s'] = 0;
direction['w'] = 1;
direction['a'] = 3;
direction['d'] = 2;
ifstream inFile;
inFile.open("Score.txt");
inFile >> name >> highestScore;
inFile.close();
ofstream outFile;
outFile.open("Score.txt");
cout << "Please enter your name = ";
getline(cin, currentName);
newGame();
while (true)
{
system("CLS");
displayBoard();
cout << "\nEnter what you want to do = ";
command = _getche();
if (command == 'n')
{
newGame();
}
else if (command == 'e')
{
break;
}
else
{
currentDirection = direction[command];
moveInDirection(currentDirection);
}
if (gameOver())
{
if (score > highestScore)
{
outFile << currentName << " " << score;
}
do {
system("CLS");
cout << "YOU HAVE LOST :("
<< "Your score was " << score << endl
<< "Do you want to play again ( New game (n) / Exit (e) ) = ";
command = _getche();
} while (command != 'n' && command != 'e');
if (command == 'e')
{
exit(1);
}
}
}
return 0;
}
void newGame()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
board[i][j] = 0;
}
}
board[0][0] = 2;
}
void displayBoard()
{
SetConsoleTextAttribute(hConsole, 9);
cout << "\n2048 THE GAME\n\n a = left , s = down , w = up , d = right , n = newgame , e = exit\n\n If all boxes get filled you lose\n\nBest player = " << name << " Best score = " << highestScore << "\n\n Your Score = " << score << "\n\n" << endl;
for (int i = 0; i < limit; i++)
{
cout << "\t\t|";
for (int j = 0; j < limit; j++)
{
if (board[i][j] == 0)
{
cout << " - |";
}
else
{
cout << " " << board[i][j] << " |";
}
}
cout << endl;
}
}
void moveInDirection(int currentDirection)
{
int count = 0;
bool flag = false;
if (currentDirection == 2)
{
while (count != 4)
{
for (int i = 0; i < limit; i++)
{
for (int j = limit - 1; j > 0; j--)
{
if ((board[i][j] == board[i][j - 1]) && board[i][j] != 0 && board[i][j - 1] != 0)
{
board[i][j] += board[i][j - 1];
board[i][j - 1] = 0;
score += board[i][j];
}
else if ((board[i][j] == 0) && (board[i][j - 1] != 0))
{
flag = true;
swap(board[i][j], board[i][j - 1]);
}
}
}
count++;
}
if (flag)
{
generateNumInBoard();
}
}
else if (currentDirection == 3)
{
while (count != 4)
{
for (int i = 0; i < limit; i++)
{
for (int j = 0; j < limit - 1; j++)
{
if ((board[i][j] == board[i][j + 1]) && board[i][j] != 0 && board[i][j + 1] != 0)
{
board[i][j] += board[i][j + 1];
board[i][j + 1] = 0;
score += board[i][j];
}
else if ((board[i][j] == 0) && (board[i][j + 1] != 0))
{
flag = true;
swap(board[i][j], board[i][j + 1]);
}
}
}
count++;
}
if (flag)
{
generateNumInBoard();
}
}
else if (currentDirection == 1)
{
while (count != 4)
{
for (int i = 0; i < limit - 1; i++)
{
for (int j = 0; j < limit; j++)
{
if ((board[i][j] == board[i + 1][j]) && board[i][j] != 0 && board[i + 1][j] != 0)
{
board[i][j] += board[i + 1][j];
board[i + 1][j] = 0;
score += board[i][j];
}
else if ((board[i][j] == 0) && (board[i + 1][j] != 0))
{
flag = true;
swap(board[i][j], board[i + 1][j]);
}
}
}
count++;
}
if (flag)
{
generateNumInBoard();
}
}
else if (currentDirection == 0)
{
while (count != 4)
{
for (int i = limit - 1; i >= 0; i--)
{
for (int j = 0; j < limit; j++)
{
if ((board[i][j] == board[i - 1][j]) && board[i][j] != 0 && board[i - 1][j] != 0)
{
board[i][j] += board[i - 1][j];
board[i - 1][j] = 0;
score += board[i][j];
}
else if ((board[i][j] == 0) && (board[i - 1][j] != 0))
{
flag = true;
swap(board[i][j], board[i - 1][j]);
}
}
}
count++;
}
if (flag)
{
generateNumInBoard();
}
}
}
int generateNum()
{
srand(time(NULL));
int randomNum = rand() % 4;
if (randomNum <= 2)
{
return 2;
}
else
{
return 4;
}
}
void generateNumInBoard()
{
bool flag = true;
for (int i = 0; i < limit; i++)
{
for (int j = 0; j < limit; j++)
{
if (board[i][j] == 0)
{
board[i][j] = generateNum();
flag = false;
break;
}
}
if (flag == false)
{
break;
}
}
}
bool gameOver()
{
for (int i = 0; i < limit; i++)
{
for (int j = 0; j < limit; j++)
{
if (board[i][j] == 0)
{
return true;
}
}
}
return false;
}
You made a small blunder at the fucntion gameOver() as you are making the game end if the compiler finds any 0 in the 2D array and you are display the zero as - in front end , You just need to make he false part true and viceversa.The game will work AOK :-)
bool gameOver()
{
for (int i = 0; i < limit; i++)
{
for (int j = 0; j < limit; j++)
{
if (board[i][j] == 0)
{
return false; //You need to make this false to not end game as soon as it starts
}
}
}
return true; //You need to make this true as the end condition
}

Printing hollow square with array

I'm trying to print a hollow square. I currently have the top, bottom, and left borders drawn but for some reason cannot get the right side to draw in the correct place. I'm sure its a simple fix but I'm new to this so sorry for noobness.
int main()
{
const int boardX = 10;
const int boardY = 10;
char gameBoard[boardX][boardY];
for (int i = 0; i != boardX; i++)
{
for (int k = 0; k != boardY; k++)
{
if (i == 0 || i == 9 || k == 0 || k == 9)
{
gameBoard[i][k] = '*';
cout << gameBoard[i][k];
}
}
cout << "\n";
}
system("pause");
return 0;
}
You forgot to print spaces to make the square hollow:
int main()
{
const int boardX = 10;
const int boardY = 10;
char gameBoard[boardX][boardY];
for (int i = 0; i != boardX; i++)
{
for (int k = 0; k != boardY; k++)
{
if (i == 0 || i == 9 || k == 0 || k == 9)
{
gameBoard[i][k] = '*';
cout << gameBoard[i][k];
}
else
{
gameBoard[i][k] = ' ';
cout << gameBoard[i][k];
}
}
cout << "\n";
}
return 0;
}
Note: I followed your style to make the changed part stand out, but normally you would separate the construction of the board and printing it to the screen into separate code blocks/functions.

How can I improve my code for Connect 4 in C++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have recently coded Connect 4 in C++. I would like to know if there are any improvements that could be made to the code.
connect4.cpp:
#include <iostream>
#include <stdlib.h>
char grid[6][7];
void setup_grid();
void display_grid();
void player_turn(char);
bool four_in_a_row(char);
bool grid_is_full();
int main()
{
char player = '1';
setup_grid();
system("cls");
while(true)
{
display_grid();
player_turn(player);
if(four_in_a_row(player))
{
system("cls");
display_grid();
std::cout << "Player " << player << " has won!" << std::endl;
goto end;
}
else if(grid_is_full())
{
system("cls");
display_grid();
std::cout << "It is a draw." << std::endl;
goto end;
}
if(player == '1')
player = '2';
else if(player == '2')
player = '1';
system("cls");
}
end:
std::cin.get();
std::cin.get();
return 0;
}
void setup_grid()
{
for(int i = 0; i < 6; i++)
for(int j = 0; j < 7; j++)
grid[i][j] = '0';
}
void display_grid()
{
std::cout << std::endl;
std::cout << "Enter a column number (1-7) to put a piece into that column." << std::endl;
std::cout << std::endl;
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 7; j++)
std::cout << grid[i][j] << ' ';
std::cout << std::endl;
}
std::cout << std::endl;
}
void player_turn(char player)
{
int column_number;
bool valid;
do
{
valid = true;
std::cout << "Player " << player << ": ";
std::cin >> column_number;
column_number--;
if((column_number < 0) || (column_number > 6))
{
std::cout << "That number was not between 1 and 7" << std::endl;
valid = false;
continue;
}
if(grid[0][column_number] != '0')
{
std::cout << "Column is full." << std::endl;
valid = false;
}
}
while(!valid);
for(int i = 0; i < 6; i++)
{
if(grid[i + 1][column_number] != '0')
{
grid[i][column_number] = player;
break;
}
else if(i == 6)
{
grid[i][column_number] = player;
break;
}
}
}
bool four_in_a_row(char player)
{
// Horizontal check:
for(int i = 0; i < 6; i++)
for(int j = 0; j < 4; j++)
if(grid[i][j] == player && grid[i][j+1] == player)
if(grid[i][j+2] == player && grid[i][j+3] == player)
return true;
// Vertical check:
for(int i = 0; i < 3; i++)
for(int j = 0; j < 7; j++)
if(grid[i][j] == player && grid[i+1][j] == player)
if(grid[i+2][j] == player && grid[i+3][j] == player)
return true;
// Diagonal check:
for(int y = 0; y < 3; y++)
{
for(int x = 0; x < 7; x++)
{
if(grid[y][x] == player)
{
// Diagonally left:
if(grid[y+1][x-1] == player)
{
if(grid[y+2][x-2] == player)
if(grid[y+3][x-3] == player)
return true;
}
// Diagonally right: (There is an error here)
if(grid[y+1][x+1] == player)
{
if(grid[y+2][x+2] == player)
if(grid[y+3][x+3] == player)
return true;
}
}
}
}
return false;
}
bool grid_is_full()
{
for(int y = 0; y < 6; y++)
{
for(int x = 0; x < 7; x++)
{
if(grid[y][x] == '0')
return false;
}
}
return true;
}
I know the goto statement (line 36) is bad programming practice but it seemed appropriate to the situation.
Should I use camelCase or snake_case?
On lines 48 and 49, why do I have to type std::cin.get() twice to get one input?
1) Put the I/J size in a #define instead of multiple use of 6 & 7.
2) Every time when a player place an entry reduce 1 from 6*7 count variable. When count variable reaches 0 grid_is_full.

compare value to array element

when i get user input cin >> x; and cin >> y;
I want to then compare this to an array "Location cord" L1[i][j], but when i use an if statement
- if (x && y == L1[i][j])
- if (x == L1[i] && y == L1[j])
Im not getting the result of "HIT" which is what i need.
This is a sort of minesweeper project. Any help, or pointers would be much appreciated!
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
char L1[8][8], L2[16][16], L3[24][24]; // grid location of cords
char B1[8][8], B2[16][16], B3[24][24]; // grid location of bombs
int i, j;
using namespace std;
// load and print grids L1 L2 L3
void loadgrid_L1()
{
for ( i = 0; i < 8; i++)
{
for ( j = 0; j < 8; j++)
{
L1[i][j] = 'X';
}
}
}
void printgrid_L1()
{
for ( i = 0; i < 8; i++)
{
for ( j = 0; j < 8; j++)
{
cout << L1[i][j];
}
cout << ("\n");
}
}
void loadgrid_L2()
{
for (i = 0; i < 16; i++)
{
for (j = 0; j < 16; j++)
{
L2[i][j] = 'X';
}
}
}
void printgrid_L2()
{
for (i = 0; i < 16; i++)
{
for (j = 0; j < 16; j++)
{
cout << L2[i][j];
}
cout << ("\n");
}
}
void loadgrid_L3()
{
for (i = 0; i < 24; i++)
{
for (j = 0; j < 24; j++)
{
L3[i][j] = 'X';
}
}
}
void printgrid_L3()
{
for (i = 0; i < 24; i++)
{
for (j = 0; j < 24; j++)
{
cout << L3[i][j];
}
cout << ("\n");
}
}
void bomb1()
{
L1[1][5] = 'O';
}
void bomb2()
{
L2[3][4] = 'O';
}
void bomb3()
{
L3[6][6] = 'O';
}
void menu() //need to work on this section
{
string lvl, x, y;
cout << "Please select a lvl to play L1, L2, L3 " << endl;
getline(cin, lvl);
if (lvl == "L1")
{
loadgrid_L1();
//moved bomb() from here
cout << "Please enter your co-ordinates for your move" << endl;
cout << "Cord 1: ";
getline(cin, x);
cout << "Cord 2: ";
getline(cin, y);
if (x == L1[i])
{
cout << "HIT!" << endl;
}
else if (x != L1[i])
{
cout << "SUCCESS!" << endl;
}
bomb1();
printgrid_L1();
}
else if (lvl == "L2")
{
loadgrid_L2();
bomb2();
printgrid_L2();
}
else if (lvl == "L3")
{
loadgrid_L3();
bomb3();
printgrid_L3();
}
else if ((lvl != "L1") && (lvl != "L2") && (lvl != "L3"))
{
cout << "You must pick a lvl" << endl;
while ((lvl != "L1") && (lvl != "L2") && (lvl != "L3"))
{
getline(cin, lvl);
if (lvl == "L1")
{
loadgrid_L1();
bomb1();
printgrid_L1();
}
else if (lvl == "L2")
{
loadgrid_L2();
bomb2();
printgrid_L2();
}
else if (lvl == "L3")
{
loadgrid_L3();
bomb3();
printgrid_L3(); // needs to stop
}
}
}
}
int main()
{
menu();
return 0;
}
You can easily change each of your loadgrid & printgrid functions to be 2 single functions as such:
void loadgrid( unsigned int lvl ) {
if ( lvl < 0 || lvl > 3 ) {
std::cout << "Error: invalid level entered: " << lvl << std::endl;
return;
}
int size = 0;
if ( lvl == 1 )
size = 8;
else if ( lvl == 2 )
size = 16;
else if ( lvl == 3 )
size = 24;
for ( int i = 0; i < size; i++ ) {
for ( int j = 0; j < size; j++ ) {
if ( lvl == 1 )
L1[i][j] = 'X';
else if ( lvl == 2 )
L2[i][j] = 'X';
else if ( lvl == 3 )
L3[i][j] = 'X';
}
}
}
And you can follow the same pattern above for print grid.
Then when you call the function you just pass what lvl you want to load and or print.