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?
Sep 26th, 2001, 01:44 AM
#2
PowerPoster
Damnedest kind of VB I ever saw....
Gentile or Jew,
O you who turn the wheel and look to windward,
Consider Phlebas, who was once handsome and tall as you...
Sep 26th, 2001, 02:17 AM
#3
Retired VBF Adm1nistrator
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
Sep 26th, 2001, 06:23 AM
#4
Frenzied Member
Damnedest kind of VB I ever saw....
Maybe beacuse it is C++.
Sep 26th, 2001, 07:27 AM
#5
PowerPoster
Really?
Oh wow! I hadn't realised!!
Gentile or Jew,
O you who turn the wheel and look to windward,
Consider Phlebas, who was once handsome and tall as you...
Sep 26th, 2001, 11:52 PM
#6
PowerPoster
TTT AI
Hey, has anyone developed an 'A.I' for tic-tac-toe.
I know it's simple, but most(ALL) TTT games I see work the computer move by running it through every board combo and making the appropriate move.
I just made one where the computer is initially VERY easy to beat, but after many games, it will learn which moves lead it to win, and which lead it to lose, and thus I am hoping it will 'learn' to make its own decisions based on its game history.
If anyone is interested, I wouldn't mind some testers...
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
Sep 27th, 2001, 02:49 AM
#7
Retired VBF Adm1nistrator
Yeah go on post the code.
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
Sep 27th, 2001, 03:53 AM
#8
PowerPoster
Here we go...
Here you go.
I am aware of some bugs, but haven't had a chance to sort them out...
I just added another section to the computer's AI which I am not sure is working correctly, but we'll see...
Be sure to set the difficulty to 'Hard' when you run the program, or the computer will make completely random moves.
If you feel like fiddling with the DB, the password is 'ttt'.
Cheers.
Attached Files
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
Sep 27th, 2001, 04:00 AM
#9
Retired VBF Adm1nistrator
Hmmm crashed with an AccessDB error when I selected Hard and then took my move
Probably because you'd hardcoded the path :
VB Code:
Const strConnection = "Provider=MSDASQL.1;Persist Security Info=False;Extended Properties='DBQ=C:\VB Projects\TTT AI\ttt.mdb;DefaultDir=C:\VB Projects\TTT AI;Driver={Microsoft Access Driver (*.mdb)};DriverId=25;FIL=MS Access;FILEDSN=C:\VB Projects\TTT AI\ttt.mdb.dsn;MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;PWD=ttt;UserCommitSync=no;';Initial Catalog=C:\VB Projects\TTT AI\ttt"
Would I be right i saying that you're basically keeping account of how successful the computer player was given certain board configurations ?
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
Sep 27th, 2001, 04:03 AM
#10
PowerPoster
Whoops... I just C&P the connection string...
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
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