| | |
TIC TAC TOE automized player....is me!!
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 22
Reputation:
Solved Threads: 0
//To check if there is a win
#include <iostream>
#include "tictactoe.h"
using namespace std;
int win(char board[3][3])
{
for(int i=0;i<3;i=i+1)
{
if ((board[i][0] == 'x' && board[i][1] == 'x' && board[i][2] == 'x')|| (board[i][0] == 'o' && board[i][1] == 'o'&& board [i][2]== 'o'))
return 1;
else if((board[0][i] == 'x' && board[1][i] == 'x' && board[2][i] == 'x')||(board[0][i] == 'o' && board[1][i] == 'o' && board[2][i] == 'o'))
return 1;
else if((board[0][0]=='x' && board[1][1] =='x' && board[2][2] =='x')||(board[0][0] == 'o' && board[1][1] == 'o' && board[2][2]=='o')||(board[0][2] =='x' && board[1][1] =='x' && board[2][0] =='x')||(board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o'))
return 1;
else
return 0;
}
// Algorithm: if any horizonal, vertical, or diagonal row is all 'x' or 'o' (not 'e'),
// a win has occurred.
return 0; // No win yet
}
// C++ Project 1 -- tictactoe.cpp
//
#include <iostream>
using namespace std;
// Included for random number generation to see who goes first
#include <time.h>
#include <stdlib.h>
// Function prototypes common to all source code files
#include "tictactoe.h"
int main()
{
char board[3][3]; // Current tic-tac-toe board
int i, j, num;
// Initialize board to empty spaces. Key: 'e' = empty; 'x' = player 1 mark; 'o' = player 2 mark
for (i=0;i<3;i++) { // rows
for (j=0;j<3;j++) { // columns
board[i][j] = 'e';
}
}
// First, use random number generator to choose which player starts first.
// If the integer number returned by rand() is even, player 1 starts first.
// Otherwise (number is odd), player 2 starts first.
//
srand((unsigned int) time(NULL));
num = rand()%2; // randomly sets num to 0 or 1
if (!num) cout << "Player 1 starts first.\n";
else cout << "Player 2 starts first.\n";
// Let the game begin! Take turns until either the board is filled (tie) or
// one player occupies an entire row, column, or diagonal. If player overwrites
// space already filled with an 'x' or 'o', that player automatically loses.
//
for (i=1;i<=9;i++) { // Board contains 9 spaces total to fill
// Make the next move, check its legality, and check for a win
if (((!num) && i%2) || (num && !(i%2))) { // Player 1
player1_move(i, board);
if (illegal_move(i, board)) {
cout << "\n*** Player 1 made an illegal move. Player 2 wins!!!\n";
return 0;
}
if (win(board)) { // New move by player 1 has resulted in a win
cout << "\n*** Player 1 has won the game!!!\n";
print_board(board);
return 0;
}
} else { // Player 2
player2_move(i, board);
if (illegal_move(i, board)) {
cout << "\n*** Player 2 made an illegal move. Player 1 wins!!!\n";
return 0;
}
if (win(board)) { // New move by player 2 has resulted in a win
cout << "\n*** Player 2 has won the game!!!\n";
print_board(board);
return 0;
}
}
} // End of "for" loop
cout << "\n*** The game is a draw!!!\n";
print_board(board);
return 0;
}
// Function to print the current board configuration
void print_board(char board[3][3])
{
int i, j;
for (i=0;i<3;i++) {
for (j=0;j<3;j++) {
if (board[i][j] == 'e') cout << "e "; // Leave blank
else cout << board[i][j] << " ";
}
cout << endl;
}
return;
}
------------------------------------------------------------------------------------------------------
// Check for illegal move overwriting previous 'x' or 'o'
// Algorithm: look for exactly (9-i) 'e' (empty) board spaces
// where i = number of moves made so far
int illegal_move(int i, char board[3][3])
{
static char oldboard[3][3];
static int firstflag = 1;
int j, k, ecount=0, newcount=0;
// Initialize oldboard, reset firstflag
if (firstflag) {
for (j=0;j<3;j++) {
for (k=0;k<3;k++) {
oldboard[j][k] = 'e';
}
}
firstflag = 0;
}
// Now check that board matches except for the one new move
// Includes "old" code to ensure correct number of x/o values present.
//
for (j=0;j<3;j++) {
for (k=0;k<3;k++) {
if (board[j][k] == 'e') ecount++;
if (oldboard[j][k] != board[j][k]) {
if (oldboard[j][k] != 'e') return(1); // Illegal to overwrite anything but 'e'
oldboard[j][k] = board[j][k];
newcount++;
}
}
}
if ((ecount == (9-i)) && (newcount == 1))
return(0); // OK
return(1); // Illegal move made
}
-------------------------------------------------------------------------------------------------------
// C++ Project 1 -- tictactoe.h
//
// Function prototypes
int illegal_move(int, char [3][3]); // Checks for an illegal board move (return 0=OK; 1=bad move)
int win(char [3][3]); // Checks whether a player has won the game (0=no win; 1=win)
void print_board(char [3][3]); // Prints the current board configuration
void player1_move(int, char [3][3]); // Player 1 (instructor) selects the next move, marked with an 'x'
void player2_move(int, char [3][3]); // Player 2 (student) selects the next move, marked with an 'o'
------------------------------------------------------------------------------------------
now player 2 is me....but here's a swist its auctually an automized me as in I have to predict and block/win against player 1 who is the instructor.I want to enter into the center postion if its my chance 1st(note that my code is devised in a way that the chance as to who gets to go 1st is random...as I was instructed) Also I want to try and win against the instructor's code
Any help guys
ASAP!
#include <iostream>
#include "tictactoe.h"
using namespace std;
int win(char board[3][3])
{
for(int i=0;i<3;i=i+1)
{
if ((board[i][0] == 'x' && board[i][1] == 'x' && board[i][2] == 'x')|| (board[i][0] == 'o' && board[i][1] == 'o'&& board [i][2]== 'o'))
return 1;
else if((board[0][i] == 'x' && board[1][i] == 'x' && board[2][i] == 'x')||(board[0][i] == 'o' && board[1][i] == 'o' && board[2][i] == 'o'))
return 1;
else if((board[0][0]=='x' && board[1][1] =='x' && board[2][2] =='x')||(board[0][0] == 'o' && board[1][1] == 'o' && board[2][2]=='o')||(board[0][2] =='x' && board[1][1] =='x' && board[2][0] =='x')||(board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o'))
return 1;
else
return 0;
}
// Algorithm: if any horizonal, vertical, or diagonal row is all 'x' or 'o' (not 'e'),
// a win has occurred.
return 0; // No win yet
}
// C++ Project 1 -- tictactoe.cpp
//
#include <iostream>
using namespace std;
// Included for random number generation to see who goes first
#include <time.h>
#include <stdlib.h>
// Function prototypes common to all source code files
#include "tictactoe.h"
int main()
{
char board[3][3]; // Current tic-tac-toe board
int i, j, num;
// Initialize board to empty spaces. Key: 'e' = empty; 'x' = player 1 mark; 'o' = player 2 mark
for (i=0;i<3;i++) { // rows
for (j=0;j<3;j++) { // columns
board[i][j] = 'e';
}
}
// First, use random number generator to choose which player starts first.
// If the integer number returned by rand() is even, player 1 starts first.
// Otherwise (number is odd), player 2 starts first.
//
srand((unsigned int) time(NULL));
num = rand()%2; // randomly sets num to 0 or 1
if (!num) cout << "Player 1 starts first.\n";
else cout << "Player 2 starts first.\n";
// Let the game begin! Take turns until either the board is filled (tie) or
// one player occupies an entire row, column, or diagonal. If player overwrites
// space already filled with an 'x' or 'o', that player automatically loses.
//
for (i=1;i<=9;i++) { // Board contains 9 spaces total to fill
// Make the next move, check its legality, and check for a win
if (((!num) && i%2) || (num && !(i%2))) { // Player 1
player1_move(i, board);
if (illegal_move(i, board)) {
cout << "\n*** Player 1 made an illegal move. Player 2 wins!!!\n";
return 0;
}
if (win(board)) { // New move by player 1 has resulted in a win
cout << "\n*** Player 1 has won the game!!!\n";
print_board(board);
return 0;
}
} else { // Player 2
player2_move(i, board);
if (illegal_move(i, board)) {
cout << "\n*** Player 2 made an illegal move. Player 1 wins!!!\n";
return 0;
}
if (win(board)) { // New move by player 2 has resulted in a win
cout << "\n*** Player 2 has won the game!!!\n";
print_board(board);
return 0;
}
}
} // End of "for" loop
cout << "\n*** The game is a draw!!!\n";
print_board(board);
return 0;
}
// Function to print the current board configuration
void print_board(char board[3][3])
{
int i, j;
for (i=0;i<3;i++) {
for (j=0;j<3;j++) {
if (board[i][j] == 'e') cout << "e "; // Leave blank
else cout << board[i][j] << " ";
}
cout << endl;
}
return;
}
------------------------------------------------------------------------------------------------------
// Check for illegal move overwriting previous 'x' or 'o'
// Algorithm: look for exactly (9-i) 'e' (empty) board spaces
// where i = number of moves made so far
int illegal_move(int i, char board[3][3])
{
static char oldboard[3][3];
static int firstflag = 1;
int j, k, ecount=0, newcount=0;
// Initialize oldboard, reset firstflag
if (firstflag) {
for (j=0;j<3;j++) {
for (k=0;k<3;k++) {
oldboard[j][k] = 'e';
}
}
firstflag = 0;
}
// Now check that board matches except for the one new move
// Includes "old" code to ensure correct number of x/o values present.
//
for (j=0;j<3;j++) {
for (k=0;k<3;k++) {
if (board[j][k] == 'e') ecount++;
if (oldboard[j][k] != board[j][k]) {
if (oldboard[j][k] != 'e') return(1); // Illegal to overwrite anything but 'e'
oldboard[j][k] = board[j][k];
newcount++;
}
}
}
if ((ecount == (9-i)) && (newcount == 1))
return(0); // OK
return(1); // Illegal move made
}
-------------------------------------------------------------------------------------------------------
// C++ Project 1 -- tictactoe.h
//
// Function prototypes
int illegal_move(int, char [3][3]); // Checks for an illegal board move (return 0=OK; 1=bad move)
int win(char [3][3]); // Checks whether a player has won the game (0=no win; 1=win)
void print_board(char [3][3]); // Prints the current board configuration
void player1_move(int, char [3][3]); // Player 1 (instructor) selects the next move, marked with an 'x'
void player2_move(int, char [3][3]); // Player 2 (student) selects the next move, marked with an 'o'
------------------------------------------------------------------------------------------
now player 2 is me....but here's a swist its auctually an automized me as in I have to predict and block/win against player 1 who is the instructor.I want to enter into the center postion if its my chance 1st(note that my code is devised in a way that the chance as to who gets to go 1st is random...as I was instructed) Also I want to try and win against the instructor's code
Any help guys
ASAP!
•
•
Join Date: Mar 2008
Posts: 22
Reputation:
Solved Threads: 0
#include <iostream>
#include "win.cpp"
using namespace std;
int win(char board[3][3])
{
for(int i=0;i<3;i=i+1)
{
if ((board[i][0] == 'x' && board[i][1] == 'x' && board[i][2] == 'x')|| (board[i][0] == 'o' && board[i][1] == 'o'&& board [i][2]== 'o'))
return 1;
else if((board[0][i] == 'x' && board[1][i] == 'x' && board[2][i] == 'x')||(board[0][i] == 'o' && board[1][i] == 'o' && board[2][i] == 'o'))
return 1;
else if((board[0][0]=='x' && board[1][1] =='x' && board[2][2] =='x')||(board[0][0] == 'o' && board[1][1] == 'o' && board[2][2]=='o')||(board[0][2] =='x' && board[1][1] =='x' && board[2][0] =='x')||(board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o'))
return 1;
else
return 0;
}
// Algorithm: if any horizonal, vertical, or diagonal row is all 'x' or 'o' (not 'e'),
// a win has occurred.
return 0; // No win yet
}-------------------------------------------------------
this is to check if 'x'||'o' is filled in all 3 rows or columns or diagnol as in board[0][0]&&board[1][1]&&[2][2]||board[0][2]&&[1][1]&&[2][0]Then its a win
#include "win.cpp"
using namespace std;
int win(char board[3][3])
{
for(int i=0;i<3;i=i+1)
{
if ((board[i][0] == 'x' && board[i][1] == 'x' && board[i][2] == 'x')|| (board[i][0] == 'o' && board[i][1] == 'o'&& board [i][2]== 'o'))
return 1;
else if((board[0][i] == 'x' && board[1][i] == 'x' && board[2][i] == 'x')||(board[0][i] == 'o' && board[1][i] == 'o' && board[2][i] == 'o'))
return 1;
else if((board[0][0]=='x' && board[1][1] =='x' && board[2][2] =='x')||(board[0][0] == 'o' && board[1][1] == 'o' && board[2][2]=='o')||(board[0][2] =='x' && board[1][1] =='x' && board[2][0] =='x')||(board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o'))
return 1;
else
return 0;
}
// Algorithm: if any horizonal, vertical, or diagonal row is all 'x' or 'o' (not 'e'),
// a win has occurred.
return 0; // No win yet
}-------------------------------------------------------
this is to check if 'x'||'o' is filled in all 3 rows or columns or diagnol as in board[0][0]&&board[1][1]&&[2][2]||board[0][2]&&[1][1]&&[2][0]Then its a win
Last edited by mnv29brt; Mar 14th, 2008 at 9:37 pm.
•
•
Join Date: Mar 2008
Posts: 22
Reputation:
Solved Threads: 0
//F// ENGR 101, Winter 2008
// C++ Project 1 -- tictactoe.h//
// Function prototypes
1)int illegal_move(int, char [3][3]); // Checks for an illegal board move (return 0=OK; 1=bad move)
2)int win(char [3][3]); // Checks whether a player has won the game (0=no win; 1=win)
3)void print_board(char [3][3]); // Prints the current board configuration
4)void player1_move(int, char [3][3]); // Player 1 (instructor) selects the next move, marked with an 'x'
5)void player2_move(int, char [3][3]); // Player 2 (student) selects the next move, marked with an 'o'unction prototypes
// C++ Project 1 -- tictactoe.h//
// Function prototypes
1)int illegal_move(int, char [3][3]); // Checks for an illegal board move (return 0=OK; 1=bad move)
2)int win(char [3][3]); // Checks whether a player has won the game (0=no win; 1=win)
3)void print_board(char [3][3]); // Prints the current board configuration
4)void player1_move(int, char [3][3]); // Player 1 (instructor) selects the next move, marked with an 'x'
5)void player2_move(int, char [3][3]); // Player 2 (student) selects the next move, marked with an 'o'unction prototypes
Last edited by mnv29brt; Mar 14th, 2008 at 9:31 pm.
•
•
Join Date: Mar 2008
Posts: 22
Reputation:
Solved Threads: 0
// ENGR 101, Winter 2008
// C++ Project 1 -- tictactoe.cpp
//
#include <iostream>
using namespace std;
// Included for random number generation to see who goes first
#include <time.h>
#include <stdlib.h>
// Function prototypes common to all source code files
#include "tictactoe.h"
int main()
{
char board[3][3]; // Current tic-tac-toe board
int i, j, num;
// Initialize board to empty spaces. Key: 'e' = empty; 'x' = player 1 mark; 'o' = player 2 mark
for (i=0;i<3;i++) { // rows
for (j=0;j<3;j++) { // columns
board[i][j] = 'e';
}
}
// First, use random number generator to choose which player starts first.
// If the integer number returned by rand() is even, player 1 starts first.
// Otherwise (number is odd), player 2 starts first.
//
srand((unsigned int) time(NULL));
num = rand()%2; // randomly sets num to 0 or 1
if (!num) cout << "Player 1 starts first.\n";
else cout << "Player 2 starts first.\n";
// Let the game begin! Take turns until either the board is filled (tie) or
// one player occupies an entire row, column, or diagonal. If player overwrites
// space already filled with an 'x' or 'o', that player automatically loses.
//
for (i=1;i<=9;i++) { // Board contains 9 spaces total to fill
// Make the next move, check its legality, and check for a win
if (((!num) && i%2) || (num && !(i%2))) { // Player 1
player1_move(i, board);
if (illegal_move(i, board)) {
cout << "\n*** Player 1 made an illegal move. Player 2 wins!!!\n";
return 0;
}
if (win(board)) { // New move by player 1 has resulted in a win
cout << "\n*** Player 1 has won the game!!!\n";
print_board(board);
return 0;
}
} else { // Player 2
player2_move(i, board);
if (illegal_move(i, board)) {
cout << "\n*** Player 2 made an illegal move. Player 1 wins!!!\n";
return 0;
}
if (win(board)) { // New move by player 2 has resulted in a win
cout << "\n*** Player 2 has won the game!!!\n";
print_board(board);
return 0;
}
}
} // End of "for" loop
cout << "\n*** The game is a draw!!!\n";
print_board(board);
return 0;
}
// Function to print the current board configuration
void print_board(char board[3][3])
{
int i, j;
for (i=0;i<3;i++) {
for (j=0;j<3;j++) {
if (board[i][j] == 'e') cout << "e "; // Leave blank
else cout << board[i][j] << " ";
}
cout << endl;
}
return;
}
// Check for illegal move overwriting previous 'x' or 'o'
// Algorithm: look for exactly (9-i) 'e' (empty) board spaces
// where i = number of moves made so far
int illegal_move(int i, char board[3][3])
{
static char oldboard[3][3];
static int firstflag = 1;
int j, k, ecount=0, newcount=0;
// Initialize oldboard, reset firstflag
if (firstflag) {
for (j=0;j<3;j++) {
for (k=0;k<3;k++) {
oldboard[j][k] = 'e';
}
}
firstflag = 0;
}
// Now check that board matches except for the one new move
// Includes "old" code to ensure correct number of x/o values present.
//
for (j=0;j<3;j++) {
for (k=0;k<3;k++) {
if (board[j][k] == 'e') ecount++;
if (oldboard[j][k] != board[j][k]) {
if (oldboard[j][k] != 'e') return(1); // Illegal to overwrite anything but 'e'
oldboard[j][k] = board[j][k];
newcount++;
}
}
}
if ((ecount == (9-i)) && (newcount == 1))
return(0); // OK
return(1); // Illegal move made
// C++ Project 1 -- tictactoe.cpp
//
#include <iostream>
using namespace std;
// Included for random number generation to see who goes first
#include <time.h>
#include <stdlib.h>
// Function prototypes common to all source code files
#include "tictactoe.h"
int main()
{
char board[3][3]; // Current tic-tac-toe board
int i, j, num;
// Initialize board to empty spaces. Key: 'e' = empty; 'x' = player 1 mark; 'o' = player 2 mark
for (i=0;i<3;i++) { // rows
for (j=0;j<3;j++) { // columns
board[i][j] = 'e';
}
}
// First, use random number generator to choose which player starts first.
// If the integer number returned by rand() is even, player 1 starts first.
// Otherwise (number is odd), player 2 starts first.
//
srand((unsigned int) time(NULL));
num = rand()%2; // randomly sets num to 0 or 1
if (!num) cout << "Player 1 starts first.\n";
else cout << "Player 2 starts first.\n";
// Let the game begin! Take turns until either the board is filled (tie) or
// one player occupies an entire row, column, or diagonal. If player overwrites
// space already filled with an 'x' or 'o', that player automatically loses.
//
for (i=1;i<=9;i++) { // Board contains 9 spaces total to fill
// Make the next move, check its legality, and check for a win
if (((!num) && i%2) || (num && !(i%2))) { // Player 1
player1_move(i, board);
if (illegal_move(i, board)) {
cout << "\n*** Player 1 made an illegal move. Player 2 wins!!!\n";
return 0;
}
if (win(board)) { // New move by player 1 has resulted in a win
cout << "\n*** Player 1 has won the game!!!\n";
print_board(board);
return 0;
}
} else { // Player 2
player2_move(i, board);
if (illegal_move(i, board)) {
cout << "\n*** Player 2 made an illegal move. Player 1 wins!!!\n";
return 0;
}
if (win(board)) { // New move by player 2 has resulted in a win
cout << "\n*** Player 2 has won the game!!!\n";
print_board(board);
return 0;
}
}
} // End of "for" loop
cout << "\n*** The game is a draw!!!\n";
print_board(board);
return 0;
}
// Function to print the current board configuration
void print_board(char board[3][3])
{
int i, j;
for (i=0;i<3;i++) {
for (j=0;j<3;j++) {
if (board[i][j] == 'e') cout << "e "; // Leave blank
else cout << board[i][j] << " ";
}
cout << endl;
}
return;
}
// Check for illegal move overwriting previous 'x' or 'o'
// Algorithm: look for exactly (9-i) 'e' (empty) board spaces
// where i = number of moves made so far
int illegal_move(int i, char board[3][3])
{
static char oldboard[3][3];
static int firstflag = 1;
int j, k, ecount=0, newcount=0;
// Initialize oldboard, reset firstflag
if (firstflag) {
for (j=0;j<3;j++) {
for (k=0;k<3;k++) {
oldboard[j][k] = 'e';
}
}
firstflag = 0;
}
// Now check that board matches except for the one new move
// Includes "old" code to ensure correct number of x/o values present.
//
for (j=0;j<3;j++) {
for (k=0;k<3;k++) {
if (board[j][k] == 'e') ecount++;
if (oldboard[j][k] != board[j][k]) {
if (oldboard[j][k] != 'e') return(1); // Illegal to overwrite anything but 'e'
oldboard[j][k] = board[j][k];
newcount++;
}
}
}
if ((ecount == (9-i)) && (newcount == 1))
return(0); // OK
return(1); // Illegal move made
•
•
Join Date: Mar 2008
Posts: 22
Reputation:
Solved Threads: 0
now player 2 is me....but here's a swist its auctually an automized me as in I have to predict and block/win against player 1 who is the instructor.I want to enter into the center postion if its my chance 1st(note that my code is devised in a way that the chance as to who gets to go 1st is random...as I was instructed) Also I want to try and win against the instructor's code
Any help guys
ASAP!
Guys I have tried and edited it,any other confusion or discomfort in reading plz tell me i'll email you or so..
but hopefully I hear from you guys
there are 3 FUNCTIONS that I am done with:
1)win.cpp
2)tictactoe.h
3)tictactoe.cpp
Now I need to create a player_2_move function...which is me predicting almost every move of player1(instructor),when I turn this in the instructor tests my code
whether it detects a win,knows when whose chance is,and also whether my player1 code is peffect!!!!and can beat her code
Any help guys
ASAP!
Guys I have tried and edited it,any other confusion or discomfort in reading plz tell me i'll email you or so..
but hopefully I hear from you guys
there are 3 FUNCTIONS that I am done with:
1)win.cpp
2)tictactoe.h
3)tictactoe.cpp
Now I need to create a player_2_move function...which is me predicting almost every move of player1(instructor),when I turn this in the instructor tests my code
whether it detects a win,knows when whose chance is,and also whether my player1 code is peffect!!!!and can beat her code
•
•
Join Date: Mar 2008
Posts: 22
Reputation:
Solved Threads: 0
FORGET THIS MESSAGE I HAE SPLIT IT UP IN THE REPLYS,EXATLY THE SAME(almost),so please make your time available in reading this,thanks so much u guys
•
•
•
•
//To check if there is a win
#include <iostream>
#include "tictactoe.h"
using namespace std;
int win(char board[3][3])
{
for(int i=0;i<3;i=i+1)
{
if ((board[i][0] == 'x' && board[i][1] == 'x' && board[i][2] == 'x')|| (board[i][0] == 'o' && board[i][1] == 'o'&& board [i][2]== 'o'))
return 1;
else if((board[0][i] == 'x' && board[1][i] == 'x' && board[2][i] == 'x')||(board[0][i] == 'o' && board[1][i] == 'o' && board[2][i] == 'o'))
return 1;
else if((board[0][0]=='x' && board[1][1] =='x' && board[2][2] =='x')||(board[0][0] == 'o' && board[1][1] == 'o' && board[2][2]=='o')||(board[0][2] =='x' && board[1][1] =='x' && board[2][0] =='x')||(board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o'))
return 1;
else
return 0;
}
// Algorithm: if any horizonal, vertical, or diagonal row is all 'x' or 'o' (not 'e'),
// a win has occurred.
return 0; // No win yet
}
// C++ Project 1 -- tictactoe.cpp
//
#include <iostream>
using namespace std;
// Included for random number generation to see who goes first
#include <time.h>
#include <stdlib.h>
// Function prototypes common to all source code files
#include "tictactoe.h"
int main()
{
char board[3][3]; // Current tic-tac-toe board
int i, j, num;
// Initialize board to empty spaces. Key: 'e' = empty; 'x' = player 1 mark; 'o' = player 2 mark
for (i=0;i<3;i++) { // rows
for (j=0;j<3;j++) { // columns
board[i][j] = 'e';
}
}
// First, use random number generator to choose which player starts first.
// If the integer number returned by rand() is even, player 1 starts first.
// Otherwise (number is odd), player 2 starts first.
//
srand((unsigned int) time(NULL));
num = rand()%2; // randomly sets num to 0 or 1
if (!num) cout << "Player 1 starts first.\n";
else cout << "Player 2 starts first.\n";
// Let the game begin! Take turns until either the board is filled (tie) or
// one player occupies an entire row, column, or diagonal. If player overwrites
// space already filled with an 'x' or 'o', that player automatically loses.
//
for (i=1;i<=9;i++) { // Board contains 9 spaces total to fill
// Make the next move, check its legality, and check for a win
if (((!num) && i%2) || (num && !(i%2))) { // Player 1
player1_move(i, board);
if (illegal_move(i, board)) {
cout << "\n*** Player 1 made an illegal move. Player 2 wins!!!\n";
return 0;
}
if (win(board)) { // New move by player 1 has resulted in a win
cout << "\n*** Player 1 has won the game!!!\n";
print_board(board);
return 0;
}
} else { // Player 2
player2_move(i, board);
if (illegal_move(i, board)) {
cout << "\n*** Player 2 made an illegal move. Player 1 wins!!!\n";
return 0;
}
if (win(board)) { // New move by player 2 has resulted in a win
cout << "\n*** Player 2 has won the game!!!\n";
print_board(board);
return 0;
}
}
} // End of "for" loop
cout << "\n*** The game is a draw!!!\n";
print_board(board);
return 0;
}
// Function to print the current board configuration
void print_board(char board[3][3])
{
int i, j;
for (i=0;i<3;i++) {
for (j=0;j<3;j++) {
if (board[i][j] == 'e') cout << "e "; // Leave blank
else cout << board[i][j] << " ";
}
cout << endl;
}
return;
}
------------------------------------------------------------------------------------------------------
// Check for illegal move overwriting previous 'x' or 'o'
// Algorithm: look for exactly (9-i) 'e' (empty) board spaces
// where i = number of moves made so far
int illegal_move(int i, char board[3][3])
{
static char oldboard[3][3];
static int firstflag = 1;
int j, k, ecount=0, newcount=0;
// Initialize oldboard, reset firstflag
if (firstflag) {
for (j=0;j<3;j++) {
for (k=0;k<3;k++) {
oldboard[j][k] = 'e';
}
}
firstflag = 0;
}
// Now check that board matches except for the one new move
// Includes "old" code to ensure correct number of x/o values present.
//
for (j=0;j<3;j++) {
for (k=0;k<3;k++) {
if (board[j][k] == 'e') ecount++;
if (oldboard[j][k] != board[j][k]) {
if (oldboard[j][k] != 'e') return(1); // Illegal to overwrite anything but 'e'
oldboard[j][k] = board[j][k];
newcount++;
}
}
}
if ((ecount == (9-i)) && (newcount == 1))
return(0); // OK
return(1); // Illegal move made
}
-------------------------------------------------------------------------------------------------------
// C++ Project 1 -- tictactoe.h
//
// Function prototypes
int illegal_move(int, char [3][3]); // Checks for an illegal board move (return 0=OK; 1=bad move)
int win(char [3][3]); // Checks whether a player has won the game (0=no win; 1=win)
void print_board(char [3][3]); // Prints the current board configuration
void player1_move(int, char [3][3]); // Player 1 (instructor) selects the next move, marked with an 'x'
void player2_move(int, char [3][3]); // Player 2 (student) selects the next move, marked with an 'o'
------------------------------------------------------------------------------------------
now player 2 is me....but here's a swist its auctually an automized me as in I have to predict and block/win against player 1 who is the instructor.I want to enter into the center postion if its my chance 1st(note that my code is devised in a way that the chance as to who gets to go 1st is random...as I was instructed) Also I want to try and win against the instructor's code
Any help guys
ASAP!
•
•
Join Date: Jan 2008
Posts: 3,831
Reputation:
Solved Threads: 501
C++ Syntax (Toggle Plain Text)
// win.cpp #include <iostream> #include "win.cpp" using namespace std; int win(char board[3][3]) { for(int i=0;i<3;i=i+1) { if ((board[i][0] == 'x' && board[i][1] == 'x' && board[i][2] == 'x')|| (board[i][0] == 'o' && board[i][1] == 'o'&& board [i][2]== 'o')) return 1; else if((board[0][i] == 'x' && board[1][i] == 'x' && board[2][i] == 'x')||(board[0][i] == 'o' && board[1][i] == 'o' && board[2][i] == 'o')) return 1; else if((board[0][0]=='x' && board[1][1] =='x' && board[2][2] =='x')||(board[0][0] == 'o' && board[1][1] == 'o' && board[2][2]=='o')||(board[0][2] =='x' && board[1][1] =='x' && board[2][0] =='x')||(board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o')) return 1; else return 0; } // Algorithm: if any horizonal, vertical, or diagonal row is all 'x' or 'o' (not 'e'), // a win has occurred. return 0; // No win yet }// ------------------------------------------------------- // this is to check if 'x'||'o' is filled in all 3 rows or columns or diagnol as in board[0][0]&&board[1][1]&&[2][2]||board[0][2]&&[1][1]&&[2][0]Then its a win
C++ Syntax (Toggle Plain Text)
//F// ENGR 101, Winter 2008 // C++ Project 1 -- tictactoe.h// // Function prototypes int illegal_move(int, char [3][3]); // Checks for an illegal board move (return 0=OK; 1=bad move) int win(char [3][3]); // Checks whether a player has won the game (0=no win; 1=win) void print_board(char [3][3]); // Prints the current board configuration void player1_move(int, char [3][3]); // Player 1 (instructor) selects the next move, marked with an 'x' void player2_move(int, char [3][3]); // Player 2 (student) selects the next move, marked with an 'o'unction prototypes
C++ Syntax (Toggle Plain Text)
// ENGR 101, Winter 2008 // C++ Project 1 -- tictactoe.cpp // #include <iostream> using namespace std; // Included for random number generation to see who goes first #include <time.h> #include <stdlib.h> // Function prototypes common to all source code files #include "tictactoe.h" int main() { char board[3][3]; // Current tic-tac-toe board int i, j, num; // Initialize board to empty spaces. Key: 'e' = empty; 'x' = player 1 mark; 'o' = player 2 mark for (i=0;i<3;i++) { // rows for (j=0;j<3;j++) { // columns board[i][j] = 'e'; } } // First, use random number generator to choose which player starts first. // If the integer number returned by rand() is even, player 1 starts first. // Otherwise (number is odd), player 2 starts first. // srand((unsigned int) time(NULL)); num = rand()%2; // randomly sets num to 0 or 1 if (!num) cout << "Player 1 starts first.\n"; else cout << "Player 2 starts first.\n"; // Let the game begin! Take turns until either the board is filled (tie) or // one player occupies an entire row, column, or diagonal. If player overwrites // space already filled with an 'x' or 'o', that player automatically loses. // for (i=1;i<=9;i++) { // Board contains 9 spaces total to fill // Make the next move, check its legality, and check for a win if (((!num) && i%2) || (num && !(i%2))) { // Player 1 player1_move(i, board); if (illegal_move(i, board)) { cout << "\n*** Player 1 made an illegal move. Player 2 wins!!!\n"; return 0; } if (win(board)) { // New move by player 1 has resulted in a win cout << "\n*** Player 1 has won the game!!!\n"; print_board(board); return 0; } } else { // Player 2 player2_move(i, board); if (illegal_move(i, board)) { cout << "\n*** Player 2 made an illegal move. Player 1 wins!!!\n"; return 0; } if (win(board)) { // New move by player 2 has resulted in a win cout << "\n*** Player 2 has won the game!!!\n"; print_board(board); return 0; } } } // End of "for" loop cout << "\n*** The game is a draw!!!\n"; print_board(board); return 0; } // Function to print the current board configuration void print_board(char board[3][3]) { int i, j; for (i=0;i<3;i++) { for (j=0;j<3;j++) { if (board[i][j] == 'e') cout << "e "; // Leave blank else cout << board[i][j] << " "; } cout << endl; } return; } // Check for illegal move overwriting previous 'x' or 'o' // Algorithm: look for exactly (9-i) 'e' (empty) board spaces // where i = number of moves made so far int illegal_move(int i, char board[3][3]) { static char oldboard[3][3]; static int firstflag = 1; int j, k, ecount=0, newcount=0; // Initialize oldboard, reset firstflag if (firstflag) { for (j=0;j<3;j++) { for (k=0;k<3;k++) { oldboard[j][k] = 'e'; } } firstflag = 0; } // Now check that board matches except for the one new move // Includes "old" code to ensure correct number of x/o values present. // for (j=0;j<3;j++) { for (k=0;k<3;k++) { if (board[j][k] == 'e') ecount++; if (oldboard[j][k] != board[j][k]) { if (oldboard[j][k] != 'e') return(1); // Illegal to overwrite anything but 'e' oldboard[j][k] = board[j][k]; newcount++; } } } if ((ecount == (9-i)) && (newcount == 1)) return(0); // OK return(1); // Illegal move made
O.K., here it is formatted and in code tags. All formatting is lost when you don't use code tags. You have a choice. The first way leaves formatting, but doesn't add line numbers or highlight syntax. The second adds the line numbers and highlights syntax.
[code]
// paste code here
[/code]
or
[code=cplusplus]
// paste code here
[/code]
Now what specifically is the problem you are having?
![]() |
Other Threads in the C++ Forum
- Previous Thread: Dynamic array of structures
- Next Thread: Reading through a vector
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






