//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[0] == 'x' && board[1] == 'x' && board[2] == 'x')|| (board[0] == 'o' && board[1] == 'o'&& board [2]== 'o'))
return 1;
else if((board[0] == 'x' && board[1] == 'x' && board[2] == 'x')||(board[0] == 'o' && board[1] == 'o' && board[2] == '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[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[j] == 'e') cout << "e "; // Leave blank
else cout << board[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!