|
-
Sep 26th, 2001, 12:12 AM
#1
Thread Starter
New Member
my pretty much final tic tac toe game...
here it is:
#include <iostream.h>
#include <ctype.h>
#include <fstream.h>
#include "board class.h"
#include "board functions.h"
//oops, no comments are included in this... my mistake!
int main(){
char quit='Y';
int turn(1);
cout<<"\t\t***************************"<<endl
<<"\t\t******* Welcome to ********"<<endl
<<"\t\t***** Cybernetic2024's ****"<<endl
<<"\t\t** (Russell Elfenbein's) **"<<endl
<<"\t\t*** Naughts and Crosses ***"<<endl
<<"\t\t***************************"<<endl<<endl<<endl;
cout<<"What is your name, player 1? - ";
cin.getline(player1,15);
cout<<endl<<endl
<<"What is your name, player 2? - ";
cin.getline(player2,15);
while(toupper(quit) != 'N'){
game_board.alter_board();
game_board.log();
cout<<"\n\n\nWould you like to play again(Y/N)? ";
cin>>quit;
game_board.reset_board();
}
return 0;
}
----------------------------------------
//Tic Tac Toe Board Class!
class Cboard{
char board[8];
int turn, wonyet;
public:
int print_board();
int alter_board();
int reset_board();
int win();
int log();
}game_board;
------------------------------------------
//Board Class functions
char player1[15], player2[15];
char * winner;
int player1_wins(0), player2_wins(0);
Cboard: rint_board(){ // prints the current instance of the board
cout<<endl<<endl<<endl
<<"\t\t1) "<<board[0]<<" | 2) "<<board[1]<<" | 3) "<<board[2]<<endl
<<"\t\t--------------------- "<<endl
<<"\t\t4) "<<board[3]<<" | 5) "<<board[4]<<" | 6) "<<board[5]<<endl
<<"\t\t---------------------"<<endl
<<"\t\t7) "<<board[6]<<" | 8) "<<board[7]<<" | 9) "<<board[8]<<endl<<endl<<endl;
return 0;
}
//-------------------------------------------------------------------
Cboard::alter_board(){ //changes the instance of the board.(add O's or X's, and pretty much game control
int move, player, turn(1), count(0);
char symbol;
wonyet = 0;
for(count=0;count<9;count++){
player = turn%2;
if(player==1){
symbol = 'X';
cout<<endl<<endl<<"where would you like to check "<<player1<<"? - ";
game_board.print_board();
}
else{
symbol = 'O';
cout<<endl<<endl<<"where would you like to check "<<player2<<"? - ";
game_board.print_board();
}
cin>>move;
while(move<1||move>9||board[(move-1)]=='O'||board[(move-1)]=='X'){
cout<<endl<<endl<<"\a### Error ### \nInput error, no such square! choose again - ";
game_board.print_board();
cin>>move;
}
board[(move-1)]=symbol;
turn++;
game_board.print_board();
wonyet = game_board.win();
if(wonyet == 1){
count = 10;
}
}
return 0;
}
//------------------------------------------------------------------
Cboard::win(){
char XorO;
int r;
for(int x=0;x<2;x++){
if(x==0){
XorO = 'X';
}
if (x==1){
XorO = 'O';
}
if(board[0]==XorO && board[1]==XorO && board[2]==XorO||board[3]==XorO && board[4]==XorO && board[5]==XorO
||board[6]==XorO && board[7]==XorO && board[8]==XorO||board[0]==XorO && board[3]==XorO && board[6]==XorO||board[1]==XorO && board[4]==XorO && board[7]==XorO
||board[2]==XorO && board[5]==XorO && board[8]==XorO||board[0]==XorO && board[4]==XorO && board[8]==XorO||board[2]==XorO && board[4]==XorO && board[6]==XorO){
if(XorO =='X'){ cout<<endl<<endl
<<player1<<" wins!\a"<<endl<<endl;
winner = player1;
player1_wins++;
}
if(XorO =='O'){ cout<<endl<<endl
<<player2<<" wins!\a"<<endl<<endl;
winner = player2;
player2_wins++;
}
r = 1;
}
}
cout<<endl
<<"\t\t"<<player1<<"'s wins = "<<player1_wins<<endl
<<"\t\t"<<player2<<"'s wins = "<<player2_wins<<endl<<endl;
return (r);
}
//--------------------------------------------------
Cboard::log(){
ofstream log_file("log.txt",ios::app);
log_file<<"\t\t1) "<<board[0]<<" | 2) "<<board[1]<<" | 3) "<<board[2]<<endl
<<"\t\t--------------------- "<<endl
<<"\t\t4) "<<board[3]<<" | 5) "<<board[4]<<" | 6) "<<board[5]<<endl
<<"\t\t---------------------"<<endl
<<"\t\t7) "<<board[6]<<" | 8) "<<board[7]<<" | 9) "<<board[8]<<endl
<<endl<<"The winner was "<<winner<<endl<<endl<<endl;
log_file.close();
return 0;
}
Cboard::reset_board(){
for(int x=0;x<9;x++){
board[x]=' ';
}
return 0;
}
------------------------------------------
and there you have it...
comments?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|