My biggest problem about the SECOND CODE is I don't know how to call my new checkBoard function. If I put cout<<checkBoard(board, orig) in the main for example, I get errors. I don't know how I can actually call this function ever since I changed it to Player instead of const int.
It's hard to test the first part of my code because my checkBoard and my copyBoard functions aren't working anymore.
/**
* Choose the next move for a player.
*
* Given a current state of a board, choose the best next move
* for a particular player.
*
* @param[in] board the current board state (Xs, Os and Nones)
* @param[in] player whose turn it is (X or O)
* @param[out] row the row that the player should choose
* @param[out] col the column that the player should choose
*
* @returns whether or not this move can result in a win
*/
bool chooseNextMove(const Player board[9], Player player, int &row, int &col){
while (checkBoard(board)=false){
copyBoard(board[9],copy[9]);//copies array
int answer=index(int &row, int &col);//See index value of array where I need to add an X or O
copy[answer]=player // put the value "player"(X or O) where the move took place
if (checkBoard(copy)==Player)//check if the new move resulted in a win
return 1;
else if
}[/code]
This is my entire code, excluding that function. I'm trying to learn to use classes and enums and it's giving me a lot of errors. It's finally compiling but I can't do much with it.
using namespace std;
#include <iostream>
enum class Player : char {
X,
O,
None,
};
Player X;
const Player board[9] = { X,X,X,X,X,X,X,X,X };
int index(int i, int j)
{
int ans = i * 3 + j;
return ans;
}
void copyBoard(const Player orig[9], Player copy[9])
{
for (int i = 0; i<9; i++)
copy[i] = orig[i];
}
Player checkBoard(const Player board[9]) {
Player a = Player::X;
Player b = Player::o/>;
Player c = Player::None;
if (board[0] == a && board[1] == a && board[2] == a)
return a;
else if (board[3] == a && board[4] == a && board[5] == a)
return a;
else if (board[6] == a && board[7] == a && board[8] == a)
return a;
else if (board[0] == a && board[3] == a && board[6] == a)
return a;
else if (board[1] == a && board[4] == a && board[7] == a)
return a;
else if (board[2] == a && board[5] == a && board[8] == a)
return a;
else if (board[0] == a && board[4] == a && board[8] == a)
return a;
else if (board[2] == a && board[4] == a && board[6] == a)
return a;
if (board[0] == b && board[1] == b && board[2] == B)/>
return b;
else if (board[3] == b && board[4] == b && board[5] == B)/>
return b;
else if (board[6] == b && board[7] == b && board[8] == B)/>
return b;
else if (board[0] == b && board[3] == b && board[6] == B)/>
return b;
else if (board[1] == b && board[4] == b && board[7] == B)/>
return b;
else if (board[2] == b && board[5] == b && board[8] == B)/>
return b;
else if (board[0] == b && board[4] == b && board[8] == B)/>
return b;
else if (board[2] == b && board[4] == b && board[6] == B)/>
return b;
else
return c;
}
int main(){
//int x;
//int y;
//cout<<index(0,1);
//cout<< chooseNextMove(board, X, x,y);
return 0;
}
Full code
helloworld135, on 03 October 2015 - 12:20 AM, said:
My biggest problem about the SECOND CODE is I don't know how to call my new checkBoard function. If I put cout<<checkBoard(board, orig) in the main for example, I get errors. I don't know how I can actually call this function ever since I changed it to Player instead of const int.
It's hard to test the first part of my code because my checkBoard and my copyBoard functions aren't working anymore.
/**
* Choose the next move for a player.
*
* Given a current state of a board, choose the best next move
* for a particular player.
*
* @param[in] board the current board state (Xs, Os and Nones)
* @param[in] player whose turn it is (X or O)
* @param[out] row the row that the player should choose
* @param[out] col the column that the player should choose
*
* @returns whether or not this move can result in a win
*/
bool chooseNextMove(const Player board[9], Player player, int &row, int &col){
while (checkBoard(board)=false){
copyBoard(board[9],copy[9]);//copies array
int answer=index(int &row, int &col);//See index value of array where I need to add an X or O
copy[answer]=player // put the value "player"(X or O) where the move took place
if (checkBoard(copy)==Player)//check if the new move resulted in a win
return 1;
else if
}
This is my entire code, excluding that function. I'm trying to learn to use classes and enums and it's giving me a lot of errors. It's finally compiling but I can't do much with it.
using namespace std;
#include <iostream>
enum class Player : char {
X,
O,
None,
};
Player X;
const Player board[9] = { X,X,X,X,X,X,X,X,X };
int index(int i, int j)
{
int ans = i * 3 + j;
return ans;
}
void copyBoard(const Player orig[9], Player copy[9])
{
for (int i = 0; i<9; i++)
copy[i] = orig[i];
}
Player checkBoard(const Player board[9]) {
Player a = Player::X;
Player b = Player::o/>/>;
Player c = Player::None;
if (board[0] == a && board[1] == a && board[2] == a)
return a;
else if (board[3] == a && board[4] == a && board[5] == a)
return a;
else if (board[6] == a && board[7] == a && board[8] == a)
return a;
else if (board[0] == a && board[3] == a && board[6] == a)
return a;
else if (board[1] == a && board[4] == a && board[7] == a)
return a;
else if (board[2] == a && board[5] == a && board[8] == a)
return a;
else if (board[0] == a && board[4] == a && board[8] == a)
return a;
else if (board[2] == a && board[4] == a && board[6] == a)
return a;
if (board[0] == b && board[1] == b && board[2] == B)/>/>
return b;
else if (board[3] == b && board[4] == b && board[5] == B)/>/>
return b;
else if (board[6] == b && board[7] == b && board[8] == B)/>/>
return b;
else if (board[0] == b && board[3] == b && board[6] == B)/>/>
return b;
else if (board[1] == b && board[4] == b && board[7] == B)/>/>
return b;
else if (board[2] == b && board[5] == b && board[8] == B)/>/>
return b;
else if (board[0] == b && board[4] == b && board[8] == B)/>/>
return b;
else if (board[2] == b && board[4] == b && board[6] == B)/>/>
return b;
else
return c;
}
int main(){
//int x;
//int y;
//cout<<index(0,1);
//cout<< chooseNextMove(board, X, x,y);
return 0;
}
I'm trying to make a basic tictactoe AIThis is the function I'm trying to create below, I don't need anything more complex yet. I have a few problems right now, I changed everything to enums and I'm getting a lot of errors in my original code.
My biggest problem about the SECOND CODE is I don't know how to call my new checkBoard function. If I put cout<<checkBoard(board, orig) in the main for example, I get errors. I don't know how I can actually call this function ever since I changed it to Player instead of const int.
It's hard to test the first part of my code because my checkBoard and my copyBoard functions aren't working anymore.
/**
* Choose the next move for a player.
*
* Given a current state of a board, choose the best next move
* for a particular player.
*
* @param[in] board the current board state (Xs, Os and Nones)
* @param[in] player whose turn it is (X or O)
* @param[out] row the row that the player should choose
* @param[out] col the column that the player should choose
*
* @returns whether or not this move can result in a win
*/
bool chooseNextMove(const Player board[9], Player player, int &row, int &col){
while (checkBoard(board)=false){
copyBoard(board[9],copy[9]);//copies array
int answer=index(int &row, int &col);//See index value of array where I need to add an X or O
copy[answer]=player // put the value "player"(X or O) where the move took place
if (checkBoard(copy)==Player)//check if the new move resulted in a win
return 1;
else if
}
This is my entire code, excluding that function. I'm trying to learn to use classes and enums and it's giving me a lot of errors. It's finally compiling but I can't do much with it.
using namespace std;
#include <iostream>
enum class Player : char {
X,
O,
None,
};
Player X;
const Player board[9] = { X,X,X,X,X,X,X,X,X };
int index(int i, int j)
{
int ans = i * 3 + j;
return ans;
}
void copyBoard(const Player orig[9], Player copy[9])
{
for (int i = 0; i<9; i++)
copy[i] = orig[i];
}
Player checkBoard(const Player board[9]) {
Player a = Player::X;
Player b = Player::o/>/>;
Player c = Player::None;
if (board[0] == a && board[1] == a && board[2] == a)
return a;
else if (board[3] == a && board[4] == a && board[5] == a)
return a;
else if (board[6] == a && board[7] == a && board[8] == a)
return a;
else if (board[0] == a && board[3] == a && board[6] == a)
return a;
else if (board[1] == a && board[4] == a && board[7] == a)
return a;
else if (board[2] == a && board[5] == a && board[8] == a)
return a;
else if (board[0] == a && board[4] == a && board[8] == a)
return a;
else if (board[2] == a && board[4] == a && board[6] == a)
return a;
if (board[0] == b && board[1] == b && board[2] == B)/>/>
return b;
else if (board[3] == b && board[4] == b && board[5] == B)/>/>
return b;
else if (board[6] == b && board[7] == b && board[8] == B)/>/>
return b;
else if (board[0] == b && board[3] == b && board[6] == B)/>/>
return b;
else if (board[1] == b && board[4] == b && board[7] == B)/>/>
return b;
else if (board[2] == b && board[5] == b && board[8] == B)/>/>
return b;
else if (board[0] == b && board[4] == b && board[8] == B)/>/>
return b;
else if (board[2] == b && board[4] == b && board[6] == B)/>/>
return b;
else
return c;
}
int main(){
//int x;
//int y;
//cout<<index(0,1);
//cout<< chooseNextMove(board, X, x,y);
return 0;
}

New Topic/Question
Reply


MultiQuote





|