Tic Tac Toe Program That LearnsI want to make a tic tac toe program that remembers the moves that all
40 Replies - 11092 Views - Last Post: 06 August 2009 - 04:48 PM
#1
Tic Tac Toe Program That Learns
Posted 02 January 2008 - 08:35 PM
Here is what I was thinking the lines in the text file would look like:
623918754
948365217
Well you get it. Each number represents a space on the tic tac toe board and the computer will see if it can use them. Any one have any help that they can give me on how to start programming the ai and such?
Replies To: Tic Tac Toe Program That Learns
#2
Re: Tic Tac Toe Program That Learns
Posted 02 January 2008 - 08:54 PM
You might want to take a look at this pdf.
You've also got to keep in mind that a perfect AI playing against a human (assuming the human doesn't do anything stupid).
I suggest you design and program your Tic-Tac-Toe game first, and then work on the AI.
#3
Re: Tic Tac Toe Program That Learns
Posted 03 January 2008 - 03:37 PM
#4
Re: Tic Tac Toe Program That Learns
Posted 03 January 2008 - 04:08 PM
http://www.cplusplus...rial/files.html
As for the AI, I suggest you get some mind mapping software ( http://freemind.sour...x.php/Main_Page ) and start brainstorming.
Your game should have a loop, part of which should be getting input from the players (the human player, and the AI).
Something like..
while(game_running)
{
switch (turn)
{
case 0:
player_move();
turn = 1;
break;
case 1:
ai_move();
turn = 0;
break;
}
}
Where ai_move() could involve calling functions like analyze_old_moves() and do_move() (just ideas).
Edit: I've attached a Freemind mindmap for part of my checkers game. It doesn't really describe a complete AI (it just enforces the rules during one player's turn), but it's a start.
You could probably do something similar for Tic-Tac-Toe.
Attached File(s)
-
checkers_mm.zip (1.14K)
Number of downloads: 267
This post has been edited by Tom9729: 03 January 2008 - 04:14 PM
#5
Re: Tic Tac Toe Program That Learns
Posted 03 January 2008 - 04:22 PM
Tom9729, on 3 Jan, 2008 - 04:08 PM, said:
http://www.cplusplus...rial/files.html
As for the AI, I suggest you get some mind mapping software ( http://freemind.sour...x.php/Main_Page ) and start brainstorming.
Your game should have a loop, part of which should be getting input from the players (the human player, and the AI).
Something like..
while(game_running)
{
switch (turn)
{
case 0:
player_move();
turn = 1;
break;
case 1:
ai_move();
turn = 0;
break;
}
}
Where ai_move() could involve calling functions like analyze_old_moves() and do_move() (just ideas).
Edit: I've attached a Freemind mindmap for part of my checkers game. It doesn't really describe a complete AI (it just enforces the rules during one player's turn), but it's a start.
You could probably do something similar for Tic-Tac-Toe.
Extremely helpful, thanks
#6
Re: Tic Tac Toe Program That Learns
Posted 03 January 2008 - 05:09 PM
This is the code I used: (yeah there are some idiotic comments, but I make them for myself so I know what does what quickly)
#include <stdlib.h>
#include <fstream>
using namespace std;
int sWin() // Make a function to save to the Win File (win.txt)
{
ofstream WinFile ("win.txt");
if (WinFile.is_open()) // Checks if win.txt is open and then writes to it if it is
{
WinFile << "Writing this to win.txt.\n";
WinFile.close();
std::cout << "win.txt was successfully written to!" << std::endl;
}
else std::cout << "Unable to open win.txt!" << std::endl;;
} // End the sWin() function
main() // Start the main() function
{
sWin(); // Run the main function
system("PAUSE"); // Make the console wait for a response from the user to close
} // End the main() function
#7
Re: Tic Tac Toe Program That Learns
Posted 03 January 2008 - 05:55 PM
You should be using <cstdlib>, not stdlib.h. And I don't know how you're getting it to run without #including <iostream>, as that's where cout is defined. system("PAUSE") is windows specific, but that's okay (sort of) if you're not required to write portable code. main() should also specify an return type, and actually return one:
int main() {
sWin();
system("PAUSE");
return 0;
}
But with those fixed, it should run properly.
What compiler are you using?
p.s. those aren't idiotic comments. overly-abundant commenting (which you haven't done) can be problematic if it makes the code hard to read, but commenting sufficiently to clarify everything for yourself and others is a very good thing
This post has been edited by jjhaag: 03 January 2008 - 05:55 PM
#8
Re: Tic Tac Toe Program That Learns
Posted 03 January 2008 - 06:39 PM
jjhaag, on 3 Jan, 2008 - 05:55 PM, said:
You should be using <cstdlib>, not stdlib.h. And I don't know how you're getting it to run without #including <iostream>, as that's where cout is defined. system("PAUSE") is windows specific, but that's okay (sort of) if you're not required to write portable code. main() should also specify an return type, and actually return one:
int main() {
sWin();
system("PAUSE");
return 0;
}
But with those fixed, it should run properly.
What compiler are you using?
p.s. those aren't idiotic comments. overly-abundant commenting (which you haven't done) can be problematic if it makes the code hard to read, but commenting sufficiently to clarify everything for yourself and others is a very good thing
Well I changed the include file names you told me to and added return 0 to the end. Here is all of the code for a 2 player tic tac toe (I used some of the code from xoax.net): But I get an error when compiling with Devc++; "parse error at end of input" line 166. Line 166 is the last line.
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
char cSquare1('1');// Make the square variables that will make up each place on the board
char cSquare2('2');
char cSquare3('3');
char cSquare4('4');
char cSquare5('5');
char cSquare6('6');
char cSquare7('7');
char cSquare8('8');
char cSquare9('9');
int iPlayerTurn('1');
bool bGameOver(true);
char cNextMove;
bool bValidMove;
char cMark;
void ShowBoard() // Make a function to show the board
{
std::cout << " " << cSquare7 << " | " << cSquare8 << " | " << cSquare9 << " " << std::endl;
std::cout << " --+---+-- "<< std::endl;
std::cout << " " << cSquare4 << " | " << cSquare5 << " | " << cSquare6 << " " << std::endl;
std::cout << " --+---+-- "<< std::endl;
std::cout << " " << cSquare1 << " | " << cSquare2 << " | " << cSquare3 << " " << std::endl;
std::cout << "------------------------" << std::endl;
} // End the ShowBoard() function
void sWin() // Make a function to save to the Win File (win.txt)
{
ofstream WinFile ("win.txt");
if (WinFile.is_open()) // Checks if win.txt is open and then writes to it if it is
{
WinFile << "Writing this to win.txt.\n";
WinFile.close();
std::cout << "win.txt was successfully written to!" << std::endl;
}
else std::cout << "Unable to open win.txt!" << std::endl;
} // End the sWin() function
int main() // Start the main() function
{
bool bGameOver(false);
while (!bGameOver) { // Start of 'if game not over' code
if (iPlayerTurn=='1') { // Make player 1 use 'X' and player 2 use 'O'
cMark = 'X'; }
else {cMark = 'O';} // Ends cMark if statement
if (iPlayerTurn=='1') { // Make player 1 use 'X' and player 2 use 'O'
cMark = 'X'; }
else {cMark = 'O';} // Ends cMark if statement
ShowBoard(); // Show the playing board
std::cout << "Player " << iPlayerTurn << "'s turn." << std::endl; // Asks player to go
std::cin >>cNextMove; // The player's move
do { // Start of 'check valid move' do while statement
if (cNextMove == '1' && cSquare1 == '1')
{cSquare1 = cMark;}
else if (cNextMove == '2' && cSquare1 == '2')
{cSquare1 = cMark;}
else if (cNextMove == '3' && cSquare1 == '3')
{cSquare1 = cMark;}
else if (cNextMove == '4' && cSquare1 == '4')
{cSquare1 = cMark;}
else if (cNextMove == '5' && cSquare1 == '5')
{cSquare1 = cMark;}
else if (cNextMove == '6' && cSquare1 == '6')
{cSquare1 = cMark;}
else if (cNextMove == '7' && cSquare1 == '7')
{cSquare1 = cMark;}
else if (cNextMove == '8' && cSquare1 == '8')
{cSquare1 = cMark;}
else if (cNextMove == '9' && cSquare1 == '9')
{cSquare1 = cMark;}
else {
ShowBoard();
std::cout << "Invalid move, please try again." << std::endl;
bValidMove = false;} while (!bValidMove) // End of 'check valid move' do while statement
bGameOver = false;
bool bWinGame = true;
// Check for end of game conditions
if (cSquare1 != '1') {
if (cSquare2 == cSquare1 && cSquare3 == cSquare1) {
bGameOver = true;
}
if (cSquare4 == cSquare1 && cSquare7 == cSquare1) {
bGameOver = true;
}
}
if (cSquare5 != '5') {
if (cSquare1 == cSquare5 && cSquare9 == cSquare5) {
bGameOver = true;
}
if (cSquare2 == cSquare5 && cSquare8 == cSquare5) {
bGameOver = true;
}
if (cSquare4 == cSquare5 && cSquare6 == cSquare5) {
bGameOver = true;
}
if (cSquare3 == cSquare5 && cSquare7 == cSquare5) {
bGameOver = true;
}
}
if (cSquare9 != '9') {
if (cSquare3 == cSquare9 && cSquare6 == cSquare9) {
bGameOver = true;
}
if (cSquare7 == cSquare9 && cSquare8 == cSquare9) {
bGameOver = true;
}
} // End of checking for end game conditions
// Check board for no win condition (draw)
if (cSquare1 != '1' && cSquare2 != '2' && cSquare3 != '3' &&
cSquare4 != '4' && cSquare5 != '5' && cSquare6 != '6' &&
cSquare7 != '7' && cSquare8 != '8' && cSquare9 != '9' && !bGameOver)
{ // End of checking for draw
if (bGameOver) {
if (bWinGame) {
std::cout << "Player " << iPlayerTurn << " wins!" << std::endl;}
char cPlayAgain;
std::cout << "Game Over, want to play again (y/n)?" << std::endl; // Play again question
std::cin >> cPlayAgain;
if (cPlayAgain == 'y') { // Play again check
bGameOver = false;
cSquare1 = '1'; // if yes then clear baord and restart
cSquare1 = '2';
cSquare1 = '3';
cSquare1 = '4';
cSquare1 = '5';
cSquare1 = '6';
cSquare1 = '7';
cSquare1 = '8';
cSquare1 = '9';} // end play again check
if (iPlayerTurn=='1') { // Switches turns between player 1 and 2
iPlayerTurn = '2';}
else {iPlayerTurn = '1';}
} // End of 'if game not over' code
return 0; // Returns value from main() function
} // End the main() function
#9
Re: Tic Tac Toe Program That Learns
Posted 03 January 2008 - 06:55 PM
#10
Re: Tic Tac Toe Program That Learns
Posted 03 January 2008 - 06:58 PM
* If you declared "using namespace std", you don't have to include the "std::" part when calling functions from there.
* I might be mistaken, but I think the reason your text file isn't being written to is because you never called "WinFile.open()".
* Your code would be a bit cleaner if you used a "char" array instead of having individual "char"'s.
* Count your braces and parentheses, somewhere you misplaced a few and that's the reason for the compiler error.
This post has been edited by Tom9729: 03 January 2008 - 07:00 PM
#11
Re: Tic Tac Toe Program That Learns
Posted 03 January 2008 - 07:04 PM
Tom9729, on 3 Jan, 2008 - 06:58 PM, said:
The opening of the file is handled in the constructor, in this case.
ofstream WinFile ("win.txt");
is functionally equivalent to:
ofstream WinFile;
WinFile.open("win.txt");
This post has been edited by jjhaag: 03 January 2008 - 07:05 PM
#12
Re: Tic Tac Toe Program That Learns
Posted 03 January 2008 - 07:16 PM
#13
Re: Tic Tac Toe Program That Learns
Posted 03 January 2008 - 07:17 PM
#14
Re: Tic Tac Toe Program That Learns
Posted 03 January 2008 - 07:24 PM
Tom9729, on 3 Jan, 2008 - 07:16 PM, said:
it depends if you ar a begginer or not
and if you are using OO or functions you normaly would not do it like that
depends on who wrote it and if they are ok with it but you would mis out on the learning expierience + it would be easer to
make your ai if you wrote the code
This post has been edited by Jingle: 03 January 2008 - 07:26 PM
#15
Re: Tic Tac Toe Program That Learns
Posted 03 January 2008 - 07:24 PM
devilsson2010, on 3 Jan, 2008 - 07:17 PM, said:
There's nothing wrong with that, assuming their code is public domain/open source. Just be sure to give them credit (eg. Don't try to make it look like you wrote the game code).
I wrote a command line Tic Tac Toe game in Java a long time ago. One of my hard disks just crashed, but if I can find it I'll post it. I know Java != C++, but IMO it's pretty similar and you should be able to translate it.
|
|

New Topic/Question
Reply




MultiQuote




|