Jump to content
  • Advertisement
Sign in to follow this  
Appmonster

Why does this randomly finish?

This topic is 3797 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am (trying) to write my first game, a game of Naugths and crosses lol. ATM I have started to make the computer AI, which atm just randomly chooses an empty (empty being being filled with null (\x00) ) element of the array grid (in ticTac.h) then it places an 'O' in the empty element. The thing is, when I run the game everything AI wise will go fine, until after a few 'moves' then it randomly ends (when it should be making a turn) main.cpp
#include <iostream>
#include <string>
using std::string;
#include "ticTac.h"
using std::cout;
using std::cin;



int main()
{
    enum TURNCHART{
         PLAYER_TURN   = 0,
         COMPUTER_TURN = 1
    };

    bool gameRunning = true;
    int  turn = PLAYER_TURN;
    ticTac newGrid;

    newGrid.printCol("Naughts and Crosses",14,true,true);

    newGrid.printCol("Enter your name: ",15,false,false);
    string name = "";
    getline(cin,name);

    newGrid.printCol("\nHello ",15,false,false);
    newGrid.printCol(name,12,false,false);
    newGrid.printCol(" lets play, you go first.",15,false,true);

    int   setX = 0;
    int   setY = 0;

    newGrid.printCol("\nTip: To execute a move type in a co odinate (ie. 0 1) and press enter!\n",13,false,true);

    newGrid.viewGrid(0);

    while(gameRunning == true){
    //This is the players turn, when it finishes we hand it over to the computer.
     if(turn == PLAYER_TURN){
       newGrid.printCol("\nYour turn...",14,false,true);
       newGrid.printCol("\nX:",10,false,false);
       setX = newGrid.inputXY();

       newGrid.printCol("\nY:",10,false,false);
       setY = newGrid.inputXY();

       int signal = newGrid.setGrid('X',setX,setY);
       newGrid.viewGrid(signal);

       newGrid.printCol("Press enter to end your turn...\n",14,false,true);

       string trash;
       getline(cin,trash);
       turn = COMPUTER_TURN;
       trash = "";
     }

     if(turn == COMPUTER_TURN){
         newGrid.aiCheck();
         newGrid.viewGrid(0);
         turn = PLAYER_TURN;
     }

    }







}


ticTac.h
#ifndef TICTAC_H_INCLUDED
#define TICTAC_H_INCLUDED
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <cctype>
#include <windows.h>

class ticTac{
  public:
   ticTac()//Constructor
    {
        initGrid();
    }

  int setGrid(char fillWith, int xLoc, int yLoc) //Used to change values of locations within the grid
   {
       fillWith = toupper(fillWith);
       const int GRID_MAX = 3;

       enum CONSTANTS_SETGRID{
                     NO_ERRORS = 0,
                     ERROR_WITH_LOCATION = 1,
                     ERROR_WITH_CHARACTER = 2,
                     ERROR_OCCUPIED = 3};

         switch((xLoc < GRID_MAX) && (yLoc < GRID_MAX))
         {
             case true:break;
                  break;
             case false: return ERROR_WITH_LOCATION;
         }
         if( grid[xLoc][yLoc] == '\x00' ){

         switch(fillWith){
             case 'O': grid[xLoc][yLoc] = fillWith;
                       return NO_ERRORS;
                       break;
             case 'X': grid[xLoc][yLoc] = fillWith;
                       return NO_ERRORS;
                       break;
              default: return ERROR_WITH_CHARACTER;
            }
           }

         else if(grid[xLoc][yLoc] != '\x00')
             {
              return ERROR_OCCUPIED;
             }
         else
              return ERROR_WITH_LOCATION;
     }

  int inputXY(){
      char loc = 'Z';

      std::cin >> loc;
      std::cin.ignore();

      int ret = 0;

      switch(loc){
          case '\x30': ret = 0;
                   break;
          case '\x31': ret = 1;
                   break;
          case '\x32': ret = 2;
                   break;
          default: ret = 3;
      }

      return ret;

  }

  int viewGrid(int signalReturn) //used to show the grid AFTER all errors have been checked
   {
       enum ERRORS{
           NO_ERRORS   = 0,
           FOUND_ERROR = 1,
           UNDEFINED   = 2};

       switch(signalReturn)
       {
           case 0: drawGrid();
                   return NO_ERRORS;
                   break;
           case 1: printCol("Location set exceeds bounds",12,true,true);
                   return FOUND_ERROR;
                   break;
           case 2: printCol("Invalid character argument",12,true,true);
                   return FOUND_ERROR;
                   break;
           case 3: printCol("Location Occupied",12,true,true);
                   break;
          default: return UNDEFINED;
       }
   }

  void clearGrid(bool sure, bool newLine)//Clear the content of the grid (if passed true) and make a new line (if passed a second true)
   {
     int count = 0;

     if(sure){
       while(count < 3)
        {
          for(int i = 0; i < 3; i++)
           {
             grid[count] = '\x00';
           }
             count++;

        }
        if(newLine){cout << endl;}
     }
   }

   void printCol(string text,int color,bool buffIt, bool newLine)
  {
     const int DEFAULT = 15;
     const string BUFFING(28,'\x00');
     HANDLE hConsole;
     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
     SetConsoleTextAttribute(hConsole,color);

     if(buffIt){
         cout << BUFFING << text;
     }
      else if(!buffIt){
      cout << text;
      }

      if(newLine){
          cout << endl;
      }

     SetConsoleTextAttribute(hConsole,DEFAULT);
  }

   //AI section
   int aiCheck()
    {
     const int TURN_FINISHED = 0;

           srand(time(0));
 int  xLoc = (rand()%2);
 int  yLoc = (rand()%2);
 bool checking = true;

 while(checking){
     if(grid[xLoc][yLoc]=='\x00')
      {
          grid[xLoc][yLoc]='O';
          checking = false;
          return TURN_FINISHED;
      } else
         {
          xLoc = (rand()%2);
          yLoc = (rand()%2);
         }
        }
    }

   private:
   char grid[3][3];

   void drawGrid() // Used to Draw the grid, but is not used by itself. It is called by viewGrid (viewGrid has error checking)
   {
      int count = 0;

      while(count < 3){
        for(int i = 0; i < 3; i++)
         {


            cout << "|"<< grid[count] << "|";
         }
            count++;
            cout << "" <<endl;
       }
    }

   void initGrid() //Used to initiate the grid
   {

       int count = 0;

       while(count < 4){
        for(int i = 0; i < 3; i++)
         {
            grid[count] = '\x00';
         }
            count++;
       }
   }
};


#endif // TICTAC_H_INCLUDED


Can any of you help me out, I just cant seem to see the problem, why would my AI only execute 3-4 times then, when it comes to the players turn just stop working?

Share this post


Link to post
Share on other sites
Advertisement
You've posted main.cpp twice. And I don't see "gameRunning" being set to false anywhere. Either way, what does the debugger tell you when you step through the code?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!