So everything in my code works so far, but I don't know why the checkWinner function doesn't work. In my Bool checkWinner function, I used bool. When I call the function I'm not sure where to put it as well.
What I tried: one place where I have tried was in my for loop in the main function. Ex:
checkWinner(board, gwinner)
However it doesn't work, the game keeps on playing when someone even has three X's or O's.
Possible problems I see: I'm not sure what's wrong, could it be my checkwinner function, or could it be my playerChoice function?
The question is why isn't a winner being picked? Thank you for the help.
#include <iostream>
#include <string>
using namespace std;
const int ROWS = 3; // For the rows
const int COLS = 3; // For the number of columns;
void showBoard(const char[][COLS], int); // Shows the board
void playerChoice(char[][COLS], char); // shows the player choice
bool checkwinner(const char[][COLS], string);
int main()
{
char board[ROWS][COLS] = { { '*', '*', '*' }, { '*', '*', '*' }, { '*', '*', '*' } };
string gwinner = " ";
showBoard(board, 3); // displays the board
for (int i = 0; i < 4; i++) {
playerChoice(board, 'X');
showBoard(board, 3);
playerChoice(board, 'O');
showBoard(board, 3);
}
cout << "The winner is: " << gwinner;
}
void showBoard(const char arr[][COLS], int SIZE)
{
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++)
cout << '|' << arr[row][col];
cout << '|' << endl;
}
}
void playerChoice(char arr[][COLS], char name)
{
int row, columns;
char board[ROWS][COLS] = { { '*', '*', '*' }, { '*', '*', '*' }, { '*', '*', '*' } };
cout << "What row would you like?\n";
cout << "Row: ";
cin >> row;
while (row < 1 || row > 3) {
cout << "Error please pick a row between 1 and 3.\n";
cout << "Row: ";
cin >> row;
cout << endl;
}
cout << "What column would you like?\n";
cout << "Column: ";
cin >> columns;
while (columns < 1 || columns > 3) {
cout << "Error,please pick a column between 1 and 3.\n";
cout << "Column: ";
cin >> columns;
cout << endl;
}
if (arr[row - 1][columns - 1] == '*') {
arr[row - 1][columns - 1] = name;
}
else {
cout << "This space is occupied.\n";
cout << "Please select again\n";
}
}
bool checkwinner(const char arr[][COLS], string gwinner)
{
bool winner = false;
if ((arr[0][0] == arr[0][1]) && (arr[0][1] == arr[0][2])) // checks row
{
winner = true;
}
else if ((arr[1][0] == arr[1][1]) && (arr[1][1] == arr[1][2])) // checks row
{
winner = true;
}
else if ((arr[2][0] == arr[2][1]) && (arr[2][1] == arr[2][2])) // checks row
{
winner = true;
}
else if ((arr[0][0] == arr[1][0]) && (arr[1][0] == arr[2][0])) // checks columns
{
winner = true;
}
else if ((arr[0][1] == arr[1][1]) && (arr[1][1] == arr[2][1])) // checks columns
{
winner = true;
}
else if ((arr[0][2] == arr[1][2]) && (arr[1][2] == arr[2][2])) // checks columns
{
winner = true;
}
else if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2])) // checks diagonal
{
winner = true;
}
else if ((arr[2][0] == arr[1][1]) && (arr[1][1] == arr[0][2])) // checks diagonal
{
winner = true;
}
else {
gwinner = "It's a tie! ";
winner = false;
}
return winner; // returns the winner
}
Related
Ive been learning C++ and I decided to make a tictactoe project that utilizes a MiniMax algorithm. I have made all the functionality for playing, including 2 player. Why doesn't my minimax algorithm when I choose to play with the AI work ? It always moves to 0 or 8 and I can't figure out why. Any help will be appreciated
TicTacToe.h
#ifndef TICTACTOE_TICTACTOEGAME_H
#define TICTACTOE_TICTACTOEGAME_H
#include <string>
#include <iostream>
#include <vector>
class TicTacToeGame {
struct move{
int score;
int index;
};
public:
const int PLAYER1_INDEX = 1;
const int PLAYER2_INDEX = 2;
const int AI_INDEX = 3;
TicTacToeGame();
void play(TicTacToeGame);
bool hasWon() const;
bool haveTied() const;
char getMarker(int, int) const;
void setMarker(int, int);
void resetMarker(int);
int getCurrentPlayer() const;
std::vector<int> possibleMoves();
private:
char board[3][3];
std::string player1_name;
std::string player2_name;
int currentPlayer;
bool isMultiplayer;
int moveCount = 0;
void init();
void printBoard();
void setPlayer1Name(std::string);
void setPlayer2Name(std::string);
std::string getPlayer1Name() const;
std::string getPlayer2Name() const;
void switchCurrentPlayer();
std::string winMessage();
int getRow(int) const;
int getColumn(int) const;
bool checkMarker(int, int);
void makeAIMove(TicTacToeGame);
move miniMax(TicTacToeGame, int);
};
#endif //TICTACTOE_TICTACTOEGAME_H
TicTacToe.cpp
#include "TicTacToeGame.h"
// Player is MINIMIZER, AI is MAXIMIZER
TicTacToeGame::move TicTacToeGame::miniMax(TicTacToeGame board, int player) {
move current, best;
int otherPlayer = (player == 1) ? 3 : 1;
if (board.hasWon()){
if(player == PLAYER1_INDEX){
best.score = 10;
return best;
} else if(player == AI_INDEX){
best.score = -10;
return best;
}
} else if(board.haveTied()){
best.score = 0;
return best;
}
if(player == AI_INDEX){
best.score = -9999999;
} else best.score = 9999999;
std::vector<int> possibleMoves = board.possibleMoves();
for (int i = 0; i < possibleMoves.size(); i++) {
board.setMarker(possibleMoves[i], player);
current = miniMax(board, otherPlayer);
current.index = possibleMoves[i];
if(player == AI_INDEX){
if(current.score > best.score) {
best = current;
}
} else
if(current.score < best.score){
best = current;
}
board.resetMarker(possibleMoves[i]);
}
std::cout << "best index: " << best.index << " best score: " << best.score << std::endl;
return best;
}
void TicTacToeGame::makeAIMove(TicTacToeGame board) {
int move = miniMax(board, getCurrentPlayer()).index;
setMarker(move, getCurrentPlayer());
std::cout << "\nThe AI has moved to " << move << std::endl;
}
std::vector<int> TicTacToeGame::possibleMoves() {
std::vector<int> possibleMoves;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if(board[i][j] != 'X' && board[i][j] != 'O'){
possibleMoves.push_back(3 * i +j);
}
}
}
return possibleMoves;
}
TicTacToeGame::TicTacToeGame() {
init();
}
// Create the board with indexes
void TicTacToeGame::init() {
currentPlayer = 2;
int k = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++, k++) {
board[i][j] = '0' + k;
}
}
}
// Prints the board int he format of a TicTacToe game
void TicTacToeGame::printBoard() {
std::cout << "\n";
for (int i = 0; i < 3; i++) {
if (i != 0) std::cout << "\n --------------------\n";
for (int j = 0; j < 3; j++) {
std::cout << "\t" << board[i][j];
if (j != 2) std::cout << "\t|";
}
}
}
// Handles the playing
void TicTacToeGame::play(TicTacToeGame game) {
std::string welcome = "\t\t*********************************************\n\t\t*\t\tWelcome to the TicTacToe game !\t\t*\n\t\t*********************************************\n\n\n";
int position = -1;
char multiplayer;
char playAgain;
std::cout << welcome;
while (true){
std::cout << "Do you want to play multiplayer ? (y/n): ";
std::cin >> multiplayer;
multiplayer = tolower(multiplayer);
// Checks if input is valid, asks for another input if not
while (std::cin.fail() || (multiplayer != 'y' && multiplayer != 'n')) {
std::cout << "Invalid entry ! Enter either y OR n: ";
std::cin.clear();
std::cin.ignore(256, '\n');
std::cin >> multiplayer;
multiplayer = tolower(multiplayer);
}
if (multiplayer == 'y') isMultiplayer = true;
else if (multiplayer == 'n') isMultiplayer = false;
break;
}
// Initializing players
std::cout << "Player 1, please type in your name: ";
std::cin >> player1_name;
// Checks if input is valid, asks for another input if not
while (std::cin.fail()) {
std::cout << "Invalid entry ! Re-enter your name: ";
std::cin.clear();
std::cin.ignore(256, '\n');
std::cin >> player1_name;
}
setPlayer1Name(player1_name);
std::cout << getPlayer1Name() << " will be X." << std::endl;
// Only initializes player 2 if it's a multiplayer game
if (isMultiplayer){
std::cout << "\nPlayer 2, please type in your name: ";
std::cin >> player2_name;
// Checks if input is valid, asks for another input if not
while (std::cin.fail()) {
std::cout << "Invalid entry ! Re-enter your name: ";
std::cin.clear();
std::cin.ignore(256, '\n');
std::cin >> player2_name;
}
setPlayer2Name(player2_name);
std::cout << getPlayer2Name() << " will be O.\n" << std::endl;
}
std::cout << "Starting game: \nINSTRUCTIONS: Select the number of the position where you want to play" << std::endl;
// Play loop
while (!hasWon()) {
printBoard();
// Alternate between players after evey move
switchCurrentPlayer();
std::cout << "\ncurrent player is " << currentPlayer << std::endl;
// Player's move
if (currentPlayer == PLAYER1_INDEX || currentPlayer == PLAYER2_INDEX){
while (true) {
std::cin >> position;
// Checks if input is an integer, asks for another input if not
while (std::cin.fail() || position < 0 || position > 8) {
std::cout << "Invalid entry ! Enter a number between 0 and 8 inclusive: ";
std::cin.clear();
std::cin.ignore(256, '\n');
std::cin >> position;
}
// Check if there's an X or O in that position already, if yes - ask for another position to if so. If no - writes X/O
if (checkMarker(getRow(position), getColumn(position))) {
setMarker(position, getCurrentPlayer());
break;
} else {
std::cout
<< "There's already an X/O in that position. Try place it in an empty spot.\nEnter a new position: ";
}
}
}
// AI's move
else if (currentPlayer == AI_INDEX){
makeAIMove(game);
}
moveCount++;
// Checks for a winner or a tie after every move
if (hasWon() || haveTied()) {
if (hasWon()) std::cout << winMessage() << std::endl;
else if(haveTied()) std::cout << "The game is a tie !" << std::endl;
std::cout << "\n\nDo you want to play again ? Enter y OR n: ";
std::cin >> playAgain;
playAgain = tolower(playAgain); // converts to lowercase
// If there's a winner ask if user wants to play another game, stops application if not
while (true) {
if (playAgain != 'y' && playAgain != 'n') {
std::cout << playAgain << std::endl;
std::cout << "Invalid expression ! Enter either y OR n: ";
std::cin >> playAgain;
playAgain = tolower(playAgain); // converts to lowercase
} else if (playAgain == 'y' || playAgain == 'n') break;
}
if (playAgain == 'y') {
std::cout << "Generating a new board " << std::endl;
init();
moveCount = 0;
} else if (playAgain == 'n') {
std::cout << "\nThank you for playing the TicTacToe game !\nExiting the game.";
break;
}
}
}
}
std::string TicTacToeGame::winMessage() {
if (getCurrentPlayer() == 1) { return "\n\n" + getPlayer1Name() + " has won !"; }
else if (getCurrentPlayer() == 2) return "\n\n" + getPlayer2Name() + "has won !";
else return "\n\nThe AI has won !";
}
// Checks for winning conditions
bool TicTacToeGame::hasWon() const {
for (int i = 0; i < 3; ++i) {
// Checking horizontals
if (getMarker(0, i) == getMarker(1, i) && getMarker(1, i) == getMarker(2, i)) {
//cout << "horizontal alignment";
return true;
}
// Chekcing verticals
if (getMarker(i, 0) == getMarker(i, 1) && getMarker(i, 1) == getMarker(i, 2)) {
//cout << "vertical alignment";
return true;
}
}
// Checking diagonals
if (getMarker(0, 0) == getMarker(1, 1) && getMarker(1, 1) == getMarker(2, 2)) return true;
if (getMarker(0, 2) == getMarker(1, 1) && getMarker(1, 1) == getMarker(2, 0)) return true;
// If no alignment
return false;
}
void TicTacToeGame::setPlayer1Name(std::string name) {
player1_name = name;
}
void TicTacToeGame::setPlayer2Name(std::string name) {
player2_name = name;
}
std::string TicTacToeGame::getPlayer1Name() const {
return player1_name;
}
std::string TicTacToeGame::getPlayer2Name() const {
return player2_name;
}
int TicTacToeGame::getCurrentPlayer() const {
return currentPlayer;
}
int TicTacToeGame::getRow(int row) const{
return row / 3;
}
int TicTacToeGame::getColumn(int column) const {
return column % 3;
}
bool TicTacToeGame::checkMarker(int row, int column) {
if (getMarker(row, column) == 'X' || getMarker(row, column) == 'O') return false;
return true;
}
char TicTacToeGame::getMarker(int row, int column) const {
return board[row][column];
}
void TicTacToeGame::setMarker(int position, int player) {
board[getRow(position)][getColumn(position)] = player == 1 ? 'X' : 'O';
}
/* Switches the play either from:
* - PLAYER1 to PLAYER2
* - PLAYER 2 to PLAYER1
* - AI to PLAYER1
* - PLAYER1 to AI
*/
void TicTacToeGame::switchCurrentPlayer() {
if (currentPlayer == PLAYER1_INDEX && !isMultiplayer){
currentPlayer = AI_INDEX;
std::cout << "\n\nIt's the AI's turn now" << std::endl;
} else if (currentPlayer == PLAYER1_INDEX) {
currentPlayer = PLAYER2_INDEX;
std::cout << "\n\nIt's " << getPlayer2Name() << "'s turn, select the position you want to move to:";
} else if (currentPlayer == PLAYER2_INDEX || currentPlayer == AI_INDEX) {
currentPlayer = PLAYER1_INDEX;
std::cout << "\n\nIt's " << getPlayer1Name() << "'s turn, select the position you want to move to:";
}
}
bool TicTacToeGame::haveTied() const {
if (moveCount == 9) return true;
return false;
}
void TicTacToeGame::resetMarker(int position) {
board[getRow(position)][getColumn(position)] = (char) position;
}
main.cpp
#include "TicTacToeGame.h"
int main() {
TicTacToeGame game;
game.play(game);
return 0;
}
This line:
std::cout << "best index: " << best.index << " best score: " << best.score << std::endl;
best.index is uninitialized when this line is printed. That is, you never set best.index to a value.
Further, you are passing an instance of TicTacBoard by value as a parameter to another instance of methods on the same class. Some of your code operates on the board parameter. Other parts work implicitly on this. You probably want to eliminate the passing around of board by value.
Change your code such that it's invoked like this in main.
TicTacToeGame game;
game.play();
And remove all the places where you pass board as a parameter.
List.cpp (class definitions)
I have been working on code that is suppose to help familiarize with classes. My code currently has a function that displays a menu of options for the users. Option [1] is suppose to add a string, option [2] is suppose to remove a string from the list, option [3] prints the string list, option [4] exits. My option [1] seems to work okay as the user is able to input one string at a time but I am having a hard time with the removal of a string. The strings are currently stored in an array of 10 elements. I believe the function I wrote for the string removal is okay as I have debugged it and it seems successful, however, I am not seeing results on my console window.
My array is located in a private class in my class: string items[MAX_ITEMS]; along with another variable called: int totalItems;
The class is then called in my main function using a switch case:
//This code snippet below is located in a separate cpp file with main
cout << "Please enter the text you want to remove: " << endl;
cin >> userInput;
list1.remove(userInput);
////////////////////////////////////////////////////////////////////
//preprocessor directives
#include <iostream>
#include <string>
//header files
#include "list.h"
using namespace std;
List::List()
{
//clear array prior to starting (set everything to NULL)
for (int i = 0; i < MAX_ITEMS; i++)
{
items[i] = " ";
}
totalItems = 0;
}
//void List::init()
//{
// string items[MAX_ITEMS];
// totalItems = 0;
//}
bool List::insert(const string& data)
{
//verifies that string is not empty, not in the list, and not full
if (data.empty() == true || inList(data) == true || isFull() == true)
{
return false;
}
else
{
// items[isFull)] = data;
// totalItems++;
items[totalItems++] = data;
return true;
}
}
bool List::isEmpty() const
{
//runs through loop to verify array is empty
for (int i = 0; i < MAX_ITEMS; i++)
{
if (items[i].empty() != true)
{
return false;
}
}
return true;
}
//identifies whether the string is full or not
bool List::isFull() const
{
if (totalItems == MAX_ITEMS)
{
return true;
}
else
{
return false;
}
}
//identifies whether the string is already in the list or not
bool List::inList(const string& theList)
{
for (int i = 0; i < MAX_ITEMS; i++)
{
if (items[i] == theList)
{
return true;
}
}
return false;
}
bool List::remove(const string& data)
{
if (inList(data) == true || data.empty() == true)
{
return false;
}
for (int i = 0; i < MAX_ITEMS; i++)
{
if (items[i] == data)
{
items[i] == " ";
for (int j = i; j < MAX_ITEMS; j++)
{
items[j] = items[j + 1];
items[MAX_ITEMS - 1] == " ";
break;
}
}
}
totalItems--;
return true;
}
//prints list
void List::printList()
{
for (int i = 0; i < MAX_ITEMS; i++)
{
cout << i << items[i] << '\t';
}
}
list_test.cpp (main.cpp)
#include <iostream>
#include <string>
//header files
#include "list.h"
using namespace std;
//function prototypes
int showSelection();
int main()
{
List list1;
string userInput = "";
int userChoice;
// list1.init();
userChoice = showSelection();
while (userChoice != 4)
{
switch(userChoice)
{
case 1:
cout << "Please enter the text you want to add: " << endl;
cin >> userInput;
list1.insert(userInput);
/*if (list1.inList(userInput) == false)
{
cout << "Text is already entered in the list!" << endl;
}*/
if (list1.isFull() == true)
{
cout << "You have entered the MAXIMUM amount of elements!" << endl;
}
break;
case 2:
cout << "Please enter the text you want to remove: " << endl;
cin >> userInput;
list1.remove(userInput);
break;
case 3:
cout << "Printed list: " << endl;
list1.printList();
break;
}
userChoice = showSelection();
}
cout << "Goodbye. Please press enter to exit." << endl;
//TESTING PURPOSES FOR FUNCTIONS
cout << list1.insert(userInput) << endl;
cout << list1.isEmpty() << endl;
cout << list1.isFull() << endl;
cout << list1.inList(userInput) << endl;
return 0;
}
/* ===========================================
Name: showSelection
Desc: displays menu for user to choose options
for their inputted string(s).
Args: none
Retn: none
=========================================== */
int showSelection()
{
int userChoice;
bool exit = false;
while (exit == false)
{
cout << "\nTo select an option, please enter the corresponding number: " << endl;
cout << "[1] to add a string" << endl;
cout << "[2] to remove a string" << endl;
cout << "[3] to print a string" << endl;
cout << "[4] to exit" << endl << endl;
cin >> userChoice;
cout << "You entered option: " << userChoice << endl;
cout << '\n';
if (userChoice == 1 || userChoice == 2 || userChoice == 3 || userChoice == 4)
{
exit = true;
}
else
{
cout << "Invalid selection" << endl;
}
}`enter code here`
return userChoice;
}
Here is how you code should probably look:
bool List::remove(const string& data) {
// only check if the list is empty so you don't nececarily go through it
// you shoudn't ask here if the given string is in the list
// because you will search for it anyway just below
if (data.empty())
return false;
for (int i = 0; i < MAX_ITEMS; ++i) {
if (items[i] == data) { // now if it was found
items[i] = " "; // set it to your empty value
--totalItems; // prefix -- is faster then postfix one (no copy is created)
for (int j = i; j < MAX_ITEMS - 1; ++j) {
// stop at j < MAX_ITEMS - 1 because you wouldn't want
// to swap last element with the next because there
// is none behind it
if (items[j + 1] == " ")
// if the next item is already empty you don't need to shift any more
return true;
// swap the next item with much more
// efficient std::swap function
std::swap(items[j], items[j + 1]);
}
return true; // value is removed and items shifted so you can now return
}
}
// if function gets to this point that means the value wasn't found
return false;
}
If you would like to make your code more efficient, I can give you more suggestions on how to do it. This above should answer your question.
Also using an array for a struct like this isn't optimal at all. Using linked listed would mean no shifting would be required.
Edit: replaced long text with a code example
Edit2: added return statement if shifting is no longer necessary
FOR me it's unclear what you want to do as unavailability of full code to run.
But I think this should work as of what I think yo want to do
bool List::remove(const string& data){
for (int i = 0; i < totalItems; i++)
{
if (items[i] == data)
{
for (int j = i; j < totalItems-1; j++)
{
items[j] = items[j+1];
}
totalItems--;
return true;
}
}
return false;
if (inList(data) == true || data.empty() == true)
{
return false;
}
If the data parameter is in your list object, you return without removing anything? this should be !inList(data)
Additionally, when you make it into the loop below this code this loop:
for (int j = i; j < MAX_ITEMS; j++)
{
items[j] = items[j + 1];
items[MAX_ITEMS - 1] == " ";
break;
}
will only execute for j=i, the "break" statement will stop execution of this inner loop, and go back to the outer loop.
EDIT: this is how I personally would go about this problem.
bool List::remove(const string& data)
{
bool retVal;
if (!inList(data) || data.empty())
{
retVal = false;
}
else{
for (int i = 0; i < MAX_ITEMS; i++)
{
if (items[i] == data)
{
items[i] = " ";
for (int j = i; j < (MAX_ITEMS - 1); j++)
{
items[j] = items[j + 1];
}
items[MAX_ITEMS-1] = " ";
}
}
totalItems--;
retVal = true;
}
return retVal;
}
I've tried to look into the already existing codes for Tic Tac Toe, and found out there's an algorithm called MiniMax, although I've understood how it works, I can't seem to make it work using 5*5 table for Tic Tac Toe.
I've an idea of how I'll do it, but I can't seem to find an approach for it.
I'm trying to make a check for every 4 moves in 1 col/row/diagonals so that there's a win there, or there's a block if it's the other player. but I can't seem to find how I can do that, I've been working on it for the past 6 hours.
#include <iostream>
#include <cstdlib>
#include <time.h>
char arr[5][5];
char player1 = 'X';
char player2 = 'O';
char player = player1;
using namespace std;
void display() {
cout << " 1 " << "2 " << "3 "<<"4 "<<"5 " << endl;
cout << " ----------------" << endl;
for (int row = 0; row < 5; row++) {
cout << row + 1 << " | ";
for (int col = 0; col < 5; col++) {
cout << arr[row][col] << " ";
}
cout << endl;
}
}
void new_turn();
int firstEmptyRow(int c){
int i;
for(i=0;i<5;i++){
if(arr[i][c])
return i;
}
}
int firstEmptyCol(int r){
int i;
for(i=0;i<5;i++){
if(arr[r][i])
return i;
}
}
bool canWin(int mat[5][5]){
int row,col;
int countSteps;
// FOR VERTICAL |
//trying to find a count of 4 moves in 1 row, so It can be won.
for(row=0;row<5;row++){
countSteps=0;
for(col=0;col<5;col++){
if( (arr[row][col] == arr[row+1][col])
&&(arr[row][col] ==player2)){
countSteps++;
}
}
if(countSteps==4){ cout << "MOVE IS WIN-ABLE" << endl; return true;}
}
return false;
}
int computer_move(){
char temp;
int test[5][5], tempo[5][5];
int row, col;
for(row=0;row<5;row++){
for(col=0;col<5;col++){
test[row][col] = arr[row][col];
tempo[row][col] = arr[row][col];
}
}
for(row=0;row<5;row++){
for(col=0;col<5;col++){
if(arr[row][col] == '-'){
temp = arr[row][col];
if(canWin(test)){ // an attempt to test the move
}
}
}
}
}
void player_move() {
if(player==player1){
int his_moveRow, his_moveCol;
cout << "please enter your move row player " << player << endl;
cin >> his_moveRow;
cout << "please enter your move col player " << player << endl;
cin >> his_moveCol;
if (his_moveRow < 0 || his_moveRow > 5 || his_moveCol < 0 || his_moveCol > 5) {
cout << "please enter a number from 1 to 5 player " << player << endl;
player_move();
}
--his_moveRow;
--his_moveCol;
if (arr[his_moveRow][his_moveCol] == '-') {
arr[his_moveRow][his_moveCol] = player;
} else {
cout << "please try again player " << player << endl;
player_move();
}
}else{
//computer move!!
cout <<"Computer Move!"<< endl;
computer_move();
}
if (player == player1) {
player = player2;
} else {
player = player1;
}
new_turn();
}
bool check_win();
void new_turn() {
display();
if (check_win() == true) {
cout << "congratulation player " << player << " you won!" << endl;
return;
} else {
int row, col, count = 0;
for (row = 0; row < 5; row++) {
for (col = 0; col < 5; col++) {
if (arr[row][col] != '-') count++;
}
}
if (count == 25) {
cout << "No one won. That's a draw." << endl;
return;
} else {
cout << "next turn" << endl;
player_move();
}
}
}
bool check_win() {
//Vertical |
int row, col;
for (row = 0; row < 5; row++) {
for (col = 0; col < 5; col++) {
if ((arr[row][col] == arr[row + 1][col] && arr[row + 1][col] == arr[row + 2][col])
&&(arr[row+2][col] == arr[row + 3][col] && arr[row + 3][col] == arr[row + 4][col])
&& arr[row][col] != '-') {
cout << "player won" << endl;
player = arr[row][col];
return true;
}
}
}
//Horizontal -
for (row = 0; row < 5; row++) {
for (col = 0; col < 5; col++) {
if ((arr[row][col] == arr[row][col + 1] && arr[row][col + 1] == arr[row][col + 2])
&&(arr[row][col+2] == arr[row][col + 3] && arr[row][col + 3] == arr[row][col + 4])
&& arr[row][col] != '-') {
cout << "player won" << endl;
player = arr[row][col];
return true;
}
}
}
//Diagonal "\"
row=0,col=0;
if((arr[row][col] == arr[row+1][col+1] && arr[row+1][col+1] == arr[row+2][col+2]
&& arr[row+2][col+2] == arr[row+3][col+3])
&&(arr[row+3][col+3] == arr[row+4][col+4]) && arr[row][col] != '-'){
cout << "player won" << endl;
player = arr[row][col];
return true;
}
// Diagonal " / "
for (row = 4; row >= 0; row--) {
for (col = 0; col < 5; col++) {
if ((arr[row][col] == arr[row - 1][col + 1] && arr[row - 1][col + 1] == arr[row - 2][col + 2]
&& arr[row - 2][col + 2] == arr[row - 3][col + 3]
&& arr[row - 3][col + 3] == arr[row - 4][col + 4]) && arr[row][col] != '-') {
cout << "player won" << endl;
player = arr[row][col];
return true;
}
}
}
return false;
}
void game_start() {
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
arr[row][col] = '-';
}
}
display();
player_move();
}
int main() {
srand(time(NULL));
game_start();
return 0;
}
I once did something similar, though not using that MiniMax algorithm. I used a comic "guide" from xkcd that tells about optimal tic tac toe moves.
If your program follows this, you can never lose, only win or tie.
https://xkcd.com/832/
The full code I am using is listed below, it is supposed to simulate a game of craps and print details to the user and allow for betting if the user desires it. Everything functions except for the actual craps game. Instead of looping only while there is not a truth value associated to crapsResult, it finds one real value and an incomprehensible string of a single negative number. Any help would be appreciated.
int main()
{
//Declare the user input variables
int gamesPlayed = 0;
char inputPrint = ' ';
char isBetting = ' ';
int startingBet = 0;
//Declare the variables used by the program
int endingBet = 0;
int currentGame = 0;
bool crapsResult;
int gamesWon = 0;
int gamesLost = 0;
double percentWon = 0;
bool detailPrint = false;
//Prompt the user to input their variables
cout << "Enter the number of games to be played: ";
cin >> gamesPlayed;
while(gamesPlayed < 1)
{
cout << " Error: must be greater than 0" << endl;
cout << "Enter the number of games to be played: ";
cin >> gamesPlayed;
cin.clear();
cin.ignore();
}
cout << "Do you wish to print details (Y/N): ";
cin >> inputPrint;
if(inputPrint == 'y' || inputPrint == 'Y')
{
detailPrint = true;
}
cout << "Do you wish to bet (Y/N): ";
cin >> isBetting;
if(isBetting == 'y' || isBetting == 'Y')
{
cout << "Enter money to start betting with: ";
cin >> startingBet;
while(startingBet < 1)
{
cout << " Error: must be greater than 0" << endl;
cout << "Enter the number of games to be played: ";
cin >> gamesPlayed;
cin.clear();
cin.ignore();
}
}
//Seed the random number generator
srand(time(NULL));
//Set a value for ending bet
if(startingBet == 0)
{
endingBet = 1;
}
else
{
endingBet = startingBet;
}
//Call playcraps to simulate the game for as many games as the user input
for(currentGame = 1; currentGame <= gamesPlayed && endingBet > 0; currentGame += 1)
{
crapsResult = NULL;
crapsResult = playCraps(currentGame, detailPrint, isBetting, startingBet);
if(crapsResult == true)
{
gamesWon += 1;
endingBet = betting(endingBet, crapsResult);
}
if(crapsResult == false)
{
gamesLost += 1;
endingBet = betting(endingBet, crapsResult);
}
if((isBetting == 'Y' || isBetting == 'y') && (detailPrint == true))
{
cout << "Money left is $" << endingBet << endl;
}
}
//Calculate the percentage of games won
percentWon = (double(gamesWon) / double(currentGame-1)) * 100.0;
//Print the results to the user
if(isBetting == 'Y' || isBetting == 'y')
{
cout << "Money at end of games is $" << endingBet << endl;
}
cout << "The number of games played is " << currentGame - 1 << endl;
cout << "The number of games won is " << gamesWon << endl;
cout << "The number of games lost is " << gamesLost << endl;
cout << "The percent of games won is " << fixed << showpoint << setprecision(3) << percentWon << endl;
}
//Simulates the roll of a single die and returns the result
int roll()
{
int rollResult = 0;
rollResult = rand() % 6 + 1;
return rollResult;
}
//Calls roll twice and returns the sum of the two results
int roll2Dice()
{
//Declare variables for this function
int rollOne = 0;
int rollTwo = 0;
int rollSum = 0;
//Find rollOne and rollTwo
rollOne = roll();
rollTwo = roll();
//Find rollSum
rollSum = rollOne + rollTwo;
return rollSum;
}
bool playCraps(int currentGame, bool detailPrint, char isBetting, int startingBet)
{
bool crapsResult = NULL;
int currentGameStorage[100];
int currentRoll = 1;
int point = roll2Dice();
int printingNumber = 0;
currentGameStorage[0] = point;
if(point == 7 || point == 11)
{
crapsResult = true;
}
else if(point == 2 || point == 3 || point == 12)
{
crapsResult = false;
}
else
{
crapsResult = NULL;
}
while(crapsResult != true && crapsResult != false)
{
currentGameStorage[currentRoll] = roll2Dice();
if(currentGameStorage[currentRoll] == point)
{
crapsResult = true;
}
else if(currentGameStorage[currentRoll] == 7)
{
crapsResult = false;
}
currentRoll += 1;
}
if(detailPrint == true)
{
cout << "Game " << currentGame << ": ";
for(printingNumber = 0; printingNumber <= currentRoll; printingNumber += 1)
{
cout << currentGameStorage[printingNumber] << " ";
}
if(crapsResult == true)
{
cout << "win";
}
else if(crapsResult == false)
{
cout << "lose";
}
cout << endl;
}
return crapsResult;
}
int betting(int endingBet, bool crapsResult)
{
if(crapsResult == true)
{
endingBet += 1;
}
else if(crapsResult == false)
{
endingBet -= 1;
}
return endingBet;
}
Just skimmed and didn't read all of your code (so there may be other things wrong too), but this line is definitely problematic:
while(crapsResult != true && crapsResult != false)
It is logically impossible for crapsResult to simultaneously be both true and false, so that loop will never be entered.
Turix got the right bug I believe, but I would put the emphasis on a different spot:
bool crapsResult = NULL;
You are trying to use crapsResult for three different values (true, false and NULL). However, NULL usually has a integer value of 0, which translates to a boolean value of false, so your loop will never be entered.
Then a second bug comes into play: currentRoll is 1 at this time, so you try to print the contents of currentGameStorage from index 0 to 1 (inclusive), currentGameStorage[1] hasn't been assigned yet. This is why you get the cryptic numer in your output. This is a general mistake: Your code always tries to print one item too much. Use < instead of <= in the loop head to fix that:
for(printingNumber = 0; printingNumber < currentRoll; printingNumber += 1)
So I am trying to program a way to replay a tic tac toe game after someone wins, loses, or ties.
So basically my attempt to get replay to work, doesnt work. If player 1 won and I type 1 to replay, it would ask player 2 for their input.
Pseudocode outline:
do {
set entire 2d array to '*'
do {
player 1 input
does game tie?
does player 1 win
player 2 input
does game tie?
does player 2 win
} while no one wins
} while replay = 1
My actual code:
//tie check, replay, use pointer notation
#include <iostream>
using namespace std;
void initialize(char [][3]);
void player1(char [][3]);
void player2(char [][3]);
void display(char [][3]);
char check(char [3][3]);
int checkWin(int);
int tie(int);
int askReplay();
int main()
{
char board[3][3];
char end = '*';
int row1, column1, row2,column2;
int replay = 0;
int turns = 0;
//replay loop
do {
//set board to *
initialize(board);
display(board);
do {
//player 1 turn
player1(board);
turns++;
display(board);
//if turns = 9 then tie
replay = tie(turns);
//check if player 1 won
end = check(board);
replay = checkWin(end);
//player 2 turn
player2(board);
turns++;
display(board);
//if turns = 9 then tie
replay = tie(turns);
//check if player 2 won
end = check(board);
replay = checkWin(end);
} while (end == '*');
} while (replay == 1);
return 0;
}
void initialize(char (*array)[3])
{
for (int i = 0;i < 3;i++)
for (int j = 0;j < 3;j++)
array[i][j] = '*';
cout << "New Game\n";
}
void player1(char (*array)[3])
{
int row1, column1;
cout << "Player 1\nRow: ";
cin >> row1;
while (row1 < 0 || row1 > 2) {
cout << "Enter a number between 0 and 2 for Row:: ";
cin >> row1;
}
cout << "Column: ";
cin >> column1;
while (column1 < 0 || column1 > 2) {
cout << "Enter a number between 0 and 2 for Column: ";
cin >> column1;
}
if (array[row1][column1] == '*')
array[row1][column1] = 'X';
else {
cout << "Space Occupied\n";
player1(array);
}
}
void player2(char (*array)[3])
{
int row2,column2;
cout << "Player 2\nRow: ";
cin >> row2;
while (row2 < 0 || row2 > 2) {
cout << "Enter a number between 0 and 2 for Row: ";
cin >> row2;
}
cout << "Column: ";
cin >> column2;
while (column2 < 0 || column2 > 2) {
cout << "Enter a number between 0 and 2 for Column: ";
cin >> column2;
}
if (array[row2][column2] == '*')
array[row2][column2] = 'O';
else {
cout << "Space Occupied\n";
player2(array);
}
}
void display(char (*array)[3])
{
for (int x = 0;x < 3;x++) {
for (int y = 0;y < 3;y++)
cout << *(*(array + x) + y) << " ";
cout << endl;
}
}
char check(char (*array)[3])
{
int i;
/* check rows */
for(i = 0; i < 3; i++)
if(array[i][0] == array[i][1] && array[i][0] == array[i][2])
return array[i][0];
/* check columns */
for(i = 0; i < 3; i++)
if(array[0][i] == array[1][i] && array[0][i] == array[2][i])
return array[0][i];
/* test diagonals */
if(array[0][0] == array[1][1] && array[1][1] == array[2][2])
return array[0][0];
if(array[0][2] == array[1][1] && array[1][1] == array[2][0])
return array[0][2];
return '*';
}
int checkWin(int over)
{
if (over == '*')
return 0;
if (over == 'X')
cout << "Player 1 Won!\n";
else if (over == 'O')
cout << "Player 2 Won!\n";
//ask if they want to play again
int answer;
answer = askReplay();
switch (answer) {
case 1:
return 1;
case 2:
cout << "Thank you for playing.\n";
exit(0);
}
}
int tie(int count)
{
if (count == 9) {
int answer;
cout << "Tie game";
answer = askReplay();
switch (answer) {
case 1:
return 1;
case 2:
cout << "Thank you for playing.\n";
exit(0);
}
}
}
int askReplay()
{
int input;
do {
cout << "Play Again?\n1.Yes\n2.No\nEnter 1 or 2: ";
cin >> input;
if (input > 2 || input < 1)
cout << "Invalid Option\n";
} while(input > 2 || input < 1);
return input;
}
It sounds like you're having troubles with your main loop.
I'd suggest making a variable that controls which player is running and just toggle that.
do
{
set entire 2d array to '*'
current player = 0
do
{
(current player + 1) input
does game tie?
does (current player + 1) win
current player = (current player + 1) % 2
}while no one wins
}while replay = 1
See if that gets you further along.
You may want to look up the Memento Design Pattern.