User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 425,983 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,639 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 3595 | Replies: 0
Reply
Join Date: Jan 2005
Location: Atlanta
Posts: 23
Reputation: Khishin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Khishin Khishin is offline Offline
Newbie Poster

Tic Tac Toe AI help, where to reset variables.

  #1  
Jan 26th, 2005
I have just started using C++ after a year or two of a BASIC language, and I wanted to do something that required some logic, but not a lot of different commands. I decided to do a tic tac toe playing program where your opponent was the program.

The way this is supposed to work is that the program goes through loops and put down pieces until the game ends, and keeping track of wins, losses, and draws in all of those possibilities, and find the best move based on the count. My question comes as pieces are put down, and after the sim-game has ended inside the counting cycle. I'm almost sure that if the spaces are not reset to empty before the next time it is used, it will interefere with the simulations. I was wondering when an where to reset the spaces.

My time on the computer is almost up for a while so I will post the start of my code and return tomorrow.

//simple AI for tic tac toe.

//load headers and namespace
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

//begin main function
int main()
{

int pos [9] ;
int turn ;
turn=9 ;
int space [9] ;
int wins [9] ;
int move [9] ;

/*

space positions

1 2 3
4 5 6
7 8 9

Winning possibilities
1 2 3
- - -
- - -

- - -
4 5 6
- - -

- - -
- - -
7 8 9

1 - -
4 - -
7 - -

- 2 -
- 5 -
- 8 -

- - 3
- - 6
- - 9

1 - -
- 5 -
- - 9

- - 3
- 5 -
7 - -


*/

//first turn
if (turn==9)
{
    //first turn, layer 1
    for (pos [1]=1; pos [1]<4; pos [1]++)
    {
        //reset all spaces to unocupied (0)
        pos [1]=0;
        pos [2]=0;
        pos [3]=0;
        pos [4]=0;
        pos [5]=0;
        pos [6]=0;
        pos [7]=0;
        pos [8]=0;
        pos [9]=0;
        //occupy chosen space with an AI piece (1)
        if (pos [1]==1) {space [1]=1;}
        if (pos [1]==1) {space [2]=1;}
        if (pos [1]==1) {space [5]=1;}
        //first turn, layer 2
        for (pos [2]=1; pos [2]<10; pos [2]++)
        {
            //check to see if chosen space is unoccupied or not
            if (space [pos [2]]=0)
            {
                //occupy space with human piece (2)
                space [pos [2]]=2;
                //first turn, layer 3
                for (pos [3]=1; pos [3]<10; pos [3]++)
                {

Thank you for whatever help I get and I am sorry for not being able to answer any questions today.
AddThis Social Bookmark Button
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:38 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC