Click here to Skip to main content
15,905,915 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: vi ctags to vc++ Pin
David Crow29-Oct-03 3:27
David Crow29-Oct-03 3:27 
GeneralDisplaying Text in Center of Client Area Pin
Hashim Saleem28-Oct-03 15:25
Hashim Saleem28-Oct-03 15:25 
GeneralRe: Displaying Text in Center of Client Area Pin
Hashim Saleem28-Oct-03 15:42
Hashim Saleem28-Oct-03 15:42 
GeneralPaint problem with child dialogs and tab control Pin
LukeV28-Oct-03 14:25
LukeV28-Oct-03 14:25 
GeneralRe: Paint problem with child dialogs and tab control Pin
LukeV28-Oct-03 14:53
LukeV28-Oct-03 14:53 
QuestionWhat can I do if ReleaseSemaphore return zero? Pin
EastDragon28-Oct-03 14:04
EastDragon28-Oct-03 14:04 
AnswerRe: What can I do if ReleaseSemaphore return zero? Pin
Alexander M.,28-Oct-03 23:36
Alexander M.,28-Oct-03 23:36 
GeneralHelp with AI, ASAP!!! Pin
Snyp28-Oct-03 12:31
Snyp28-Oct-03 12:31 
This is what I have to work with... I have a program of tic tac toe that is in the console, command prompt Wink | ;) , and the board is an array and have everything finished up to the point of getting the computer move. I am trying first to make the computer do random moves and then record to a seperate file, one for each win, lose, and draw and learn from which moves were good and which weren't as good. So far I have the below: (please help)
#include <iostream>
#include <fstream>
using namespace std;

char matrix [3][3]; //Tic Tac Toe Matrix

char check();
void init_matrix();
void get_player_move();
void get_computer_move();
void disp_matrix();
void f_matrix();

fstream SaveFile("wx.txt",ios::in | ios::out | ios::trunc);
fstream SaveFile2("wo.txt",ios::in | ios::out | ios::trunc);

int main()
{
char done = ' ';

init_matrix();

do
{
disp_matrix();
get_player_move();
done = check(); //See if any winner
f_matrix();
if(done != ' ')
{
break; //Winner!!!
}
get_computer_move();
done = check(); //See if any winner again
f_matrix();
} while(done == ' ');

disp_matrix(); //Display final matrix

if(done=='X')
{
SaveFile << "winner X\n;" << endl;
cout << "You won!\n";
}
else
{
SaveFile2 << "winner O\n;" << endl;
cout << "I won!!!!\n";
}

// f_matrix(); //Write to file final matrix / result
SaveFile.close();
SaveFile2.close();

return 0;
}

//Initialize the matrix
void init_matrix()
{
int i, j;

for(i=0; i<3; i++)
for(j=0; j<3; j++) matrix[i][j] = ' ';
}

//Get the player's move
void get_player_move()
{
int x, y;

cout << "Enter X,Y coordinates for your move: ";
cin >> x;
cin >> y;

x--; y--;

if(matrix[x][y]!= ' ')
{
cout << "Invalid move, try again.\n";
get_player_move();
}
else
{
matrix[x][y] = 'X';
}
}

//Get a move from the computer
void get_computer_move()
{
int i, j;

do
{
i = rand() % 3;
j = rand() % 3;
}while(matrix[i][j] != ' ');


if(i*j==9)
{
cout << "draw\n";
system("pause");
}
else
{
matrix[i][j] = 'O';
}
}

//Display the matrix
void disp_matrix()
{
int t;

for(t=0; t<3; t++) {
cout << matrix[t][0] << " | " << matrix[t][1] << " | " << matrix[t][2];
if(t != 2)
{
cout << "\n---|---|---\n";
}
}
cout << "\n";
}

//See if any winner
char check()
{
int i;

// New checking system
// check rows
for(i=0; i<3; i++)
if(matrix[i][0]==matrix[i][1] && matrix[i][0]==matrix[i][2])
{
return matrix[i][0];
}

// check columns
for(i=0; i<3; i++)
if(matrix[0][i]==matrix[1][i] && matrix[0][i]==matrix[2][i])
{
return matrix[0][i];
}

//Test diagonals for winner
if(matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2])
{
return matrix[0][0];
}

if(matrix[0][2]==matrix[1][1] && matrix[1][1]==matrix[2][0])
{
return matrix[0][2];
}

/*
// Old checking system
// Check rows
if(matrix[0][0] == matrix[0][1] && matrix[0][0] == matrix[0][2])
{
return matrix[0][0];
}
if(matrix[1][0] == matrix[1][1] && matrix[1][0] == matrix[1][2])
{
return matrix[1][0];
}
if(matrix[2][0] == matrix[2][1] && matrix[2][0] == matrix[2][2])
{
return matrix[2][0];
}
// Check columns
if(matrix[0][0] == matrix[1][0] && matrix[0][0] == matrix[2][0])
{
return matrix[0][0];
}
if(matrix[0][1] == matrix[1][1] && matrix[0][1] == matrix[2][1])
{
return matrix[0][1];
}
if(matrix[0][2] == matrix[1][2] && matrix[0][2] == matrix[2][2])
{
return matrix[0][2];
}
// Check diagonal (left to right)
if(matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2])
{
return matrix[0][0];
}
// Check diagonal (right to left)
if(matrix[0][2]==matrix[1][1] && matrix[1][1]==matrix[2][0])
{
return matrix[0][2];
}
*/

return ' ';
}

void f_matrix()
{
int t;

for(t=0; t<3; t++)
{
SaveFile << matrix[t][0] << matrix[t][1] << matrix[t][2] << "\n";
}
}

<marquee>Universal Project... Soon to be a .net</marquee>
GeneralRe: Help with AI, ASAP!!! Pin
Blake Coverett28-Oct-03 17:56
Blake Coverett28-Oct-03 17:56 
GeneralRe: Help with AI, ASAP!!! Pin
Michael P Butler29-Oct-03 1:46
Michael P Butler29-Oct-03 1:46 
GeneralRe: Help with AI, ASAP!!! Pin
Atlantys30-Oct-03 12:49
Atlantys30-Oct-03 12:49 
GeneralRe: Help with AI, ASAP!!! Pin
Snyp29-Oct-03 15:18
Snyp29-Oct-03 15:18 
GeneralRe: Help with AI, ASAP!!! Pin
Snyp29-Oct-03 15:19
Snyp29-Oct-03 15:19 
QuestionDirectX/Win32 stealing key messages? Pin
andy5528-Oct-03 12:12
andy5528-Oct-03 12:12 
GeneralNeed help displaying Chinese characters in a CListCtrl Pin
Raymond So28-Oct-03 11:48
Raymond So28-Oct-03 11:48 
GeneralRe: Need help displaying Chinese characters in a CListCtrl Pin
yndfcd28-Oct-03 13:26
yndfcd28-Oct-03 13:26 
GeneralRe: Need help displaying Chinese characters in a CListCtrl Pin
Michael Dunn28-Oct-03 16:56
sitebuilderMichael Dunn28-Oct-03 16:56 
GeneralRe: How to update dialog title when a value is changed in a child window? Pin
ElizabethC28-Oct-03 11:00
ElizabethC28-Oct-03 11:00 
GeneralRe: How to update dialog title when a value is changed in a child window? Pin
David Crow28-Oct-03 11:09
David Crow28-Oct-03 11:09 
GeneralRe: How to update dialog title when a value is changed in a child window? Pin
Terry O'Nolley28-Oct-03 12:28
Terry O'Nolley28-Oct-03 12:28 
GeneralRe: How to update dialog title when a value is changed in a child window? Pin
ElizabethC28-Oct-03 13:07
ElizabethC28-Oct-03 13:07 
GeneralRe: How to update dialog title when a value is changed in a child window? Pin
Terry O'Nolley28-Oct-03 13:51
Terry O'Nolley28-Oct-03 13:51 
GeneralRe: How to update dialog title when a value is changed in a child window? Pin
ElizabethC30-Oct-03 10:35
ElizabethC30-Oct-03 10:35 
Generalupgrading vc6 c++ project to .net Pin
mindows28-Oct-03 10:49
mindows28-Oct-03 10:49 
GeneralRe: upgrading vc6 c++ project to .net Pin
Dave Bryant28-Oct-03 13:12
Dave Bryant28-Oct-03 13:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.