QuestionTic Tac Toe Ai Problems

Posts 1–5 of 5 · Page 1 of 1
Tic Tac Toe Ai Problems
Hello, I recently wrote a simple Tic Tac Toe game which features two player and single player modes.
While writing the AI for the single player option, I came across 2 problems.

How do I generate a random number excluding previous integers entered already (using rand ())?
And how do prevent the AI or second player from overwriting the previous player's entry?

Explained solutions would be most appreciated.
http://www.mpgh.net/forum/31-c-c-pro...om-struct.html
bool PutSpot() {
Random mySpot;
int mineSpot = mySpot.Next(0,3);
int mineSpot2 = mySpot.Next(0,3);
if(board[mineSpot] == 0 && board[mineSpot2] == 0) {
PutTheXAtLocation(mineSpot,mineSpot2);
return true;
}
return false;
}
so you do at computer playtime:
while(PutSpot == false) turn = computerTurn;
I don't know how you did the board, but you can loop and check it like this.
Hope I helped. (I'm little rusty)
A linear congruential generator will not generate the same number twice in one cycle. What this means if that if you have this code:


Code:
int i = rand() % 10;
and i happens to be 2, then i can't be 2 again until at least 10 other calls to rand() have been made.
Thank you, so much simpler
@Inphinity If the thread is solved please ask to a minion close it else it can help.
Random struct :
Code:
 
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

struct Random {
Random();
Random(int);
int Next();
int Next(int);
int Next(int , int );
double NextDouble();  
char* NextBytes(char[]);
char* NextBytes(int len);
char* NextCharArray(int len);
private:
       int Seed;   
};

Random::Random(){
 Seed = (int) time(0);                
}

Random::Random(int seed){
Seed =  seed;                 
}

int Random::Next() { 

return rand() % RAND_MAX;   
}

int Random::Next(int nxt) {

nxt++; 
return rand() % nxt ;                 
}

int Random::Next(int min, int max) {

if(min > max) // If min > max then swap their values
{
       int temp = max;
       max = min;
       min = temp;
}
return (rand() % (max-min + 1)) + min;
}

double Random::NextDouble() { 

//return (double)((rand()%10 + 1)*0.1) ;  Returns exactly values e.g : 0.1 , 0.4 , 0.8
return static_cast<double>(rand())/RAND_MAX;             
}

char* Random::NextBytes(char array[]) {    
string s;
for(int i = 0; i < strlen(array);i++)
{
int aids = rand() % 255 + 1;
char buffer[16];
sprintf(buffer,"%d",aids);
s += buffer;  
}      
return (char*) s.c_str();
}

char* Random::NextBytes(int len ) {   

string s;
for(int i = 0; i < len;i++)
{
int aids = rand() % 255 + 1;
char buffer[16];
sprintf(buffer,"%d",aids);
s += buffer;  
}      
return (char*) s.c_str();
}

//Credits of generating char array go to Jason
char* Random::NextCharArray(int len) { 
     char *str = (char*)calloc(len + 1, sizeof(char));
    for (int i = 0; i < len; i++)
        str[i] = (char)(rand() % 0x5E + 0x21);
    return str;     
}
Example code ( stupid but still a example ) :
Code:
 #include <iostream>
#include <random.h>
using namespace std;

void PlayAtLocation(int, int[]);

int main() { 
Random randx = Random(777);
int board[] = { 0,0,0,0,0,0,0,0,0};

PlayAtLocation(randx.Next(0,9), board);
cin.get();  

PlayAtLocation(randx.Next(0,9), board);
cin.get();   

PlayAtLocation(randx.Next(0,9), board);
cin.get();   

PlayAtLocation(randx.Next(0,9), board);
cin.get();   

PlayAtLocation(randx.Next(0,9), board);
cin.get();   

PlayAtLocation(randx.Next(0,9), board);
cin.get();   

PlayAtLocation(randx.Next(0,9), board);
cin.get();   
PlayAtLocation(randx.Next(0,9), board);
cin.get();   
PlayAtLocation(randx.Next(0,9), board);
cin.get();   
cin.get();
return 0;
}

void PlayAtLocation(int location,int bd[]) {
     if(bd[location] == 0 ) { 
     bd[location] = 1;
     cout <<  "Played at location: " << location;
     } else { 
            cout << "Already played at this location! (" << location <<")";
     }
}
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us