Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 01:00

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Tic Tac Toe: Problem with computer v player

Discussion on Tic Tac Toe: Problem with computer v player within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
LordGragen.'s Avatar
 
elite*gold: 0
Join Date: Dec 2012
Posts: 606
Received Thanks: 68
Tic Tac Toe: Problem with computer v player

hello guys, i made a tic tac toe game that player is vs computer but now i am trying to make the computer smarter so if player have 2 boxes marked and about to win computer next move should block it, or if the computer have chance to win it should.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <iostream>


using namespace std;


class TicTacToe
{
   private:
      int Position[9];
      int Player;
      int TurnCounter;
      int NumberOfPlayers;
      bool PlayerType[20];
   public:
      TicTacToe();
      void PrintTurn();
      bool PlayerHuman();
      void HumanMove();
      void ComputerMove();
      void DrawBoard();
      bool Winner();
      bool FullBoard();
      void NextTurn();

	    
}; 


TicTacToe::TicTacToe()
{
    srand(time(0)); 
    Player = 1; 
    TurnCounter = 0; 
    int i = 0;
    

    NumberOfPlayers = 2;
    PlayerType[1] = 1; 
    PlayerType[2] = 0; 

    for ( i = 0; i < 9; i++)
    {
       Position[i] = 0;
    }
}

void TicTacToe::DrawBoard()
{
	system("cls");
          cout << "+----------------------------+" << endl;
          cout << "|Tic Tac Toe |" << endl;
          cout << "|Made By: Boris Grigoryan |" << endl;
          cout << "|____________________________|" << endl;
          cout << endl;
   cout << endl
        << Position[0] << " | " << Position[1] << " | " << Position[2]
        << "\n--+---+--\n"
        << Position[3] << " | " << Position[4] << " | " << Position[5]
        << "\n--+---+--\n"
        << Position[6] << " | " << Position[7] << " | " << Position[8]
        << endl;
} 

void TicTacToe::PrintTurn()
{
   cout << "\nPlayer " << Player << "'s turn.\n";
} 

void TicTacToe::NextTurn()
{
   TurnCounter++;

   if (++Player > NumberOfPlayers)
   {
      Player = 1;
   }
} 

bool TicTacToe::PlayerHuman()
{
   return PlayerType[Player];
} 

void TicTacToe::HumanMove()
{
   cout << "\nEnter your move (0-8): ";
   int Move;

   do
   {
      do{
 std::cout << "Enter a value: ";
 while(!(std::cin >> Move)){
  std::cin.clear();
  std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  std::cout << "Error, please enter a new value: ";
 }
} while (Move < 0 || Move > 8);
   }
   while (Move < 0 || Move > 8 || Position[Move] != 0);
   Position[Move] = Player;
}

void TicTacToe::ComputerMove()
{
   int Move;

   do
   {
      Move  = rand() % 9;
   }
   while (Move < 0 || Move > 8 || Position[Move] != 0);
   Position[Move] = Player;

   
} 

bool TicTacToe::Winner()
{
   int i = 0;
   int Board[8][3] = {{0,1,2},
                     {3,4,5},
                     {6,7,8},
                     {0,3,6},
                     {1,4,7},
                     {2,5,8},
                     {0,4,8},
                     {2,4,6}}; 

   for ( i = 0; i < 8; i++) 
   {
      if ((Position[Board[i][0]] == Position[Board[i][1]]) && (Position[Board[i][1]] == Position[Board[i][2]]) && Position[Board[i][0]] != 0)
      {
         cout << "\nPlayer " << Position[Board[i][0]]
              <<  " wins!\n\n";
         cout << "Press Enter to Close...." << endl;
         system("pause");
         return 1;  
      }
   }
   return 0; 
} 

bool TicTacToe::FullBoard()
{
   if (TurnCounter == 9)
   {
      cout << "\nTie game!\n\n";
      return 1;
   }
   else
   {
      return 0;
   }
} 


int main(void)
{
   TicTacToe NewGame;
  
        
   NewGame.DrawBoard();
   
   do
   {
      NewGame.PrintTurn();
      if (NewGame.PlayerHuman()) 
      {
         NewGame.HumanMove();
      }
      else 
      {
         NewGame.ComputerMove();
      }
      
      NewGame.DrawBoard();
      NewGame.NextTurn();
    }
    while (!NewGame.Winner() && !NewGame.FullBoard()); 

    return 0;
}
LordGragen. is offline  
Old 12/02/2013, 08:05   #2
 
elite*gold: 46
Join Date: Oct 2010
Posts: 782
Received Thanks: 525
Just Check it with Some if's. If None of them succeeds do a random move. Dont know where the Problem is.
th0rex is offline  
Old 12/02/2013, 12:40   #3
 
LordGragen.'s Avatar
 
elite*gold: 0
Join Date: Dec 2012
Posts: 606
Received Thanks: 68
Quote:
Originally Posted by omitma View Post
Just Check it with Some if's. If None of them succeeds do a random move. Dont know where the Problem is.
idk how start it lol a bit confused you think you can give me example?
LordGragen. is offline  
Old 12/02/2013, 18:23   #4
 
.SkyneT.'s Avatar
 
elite*gold: 273
Join Date: Sep 2010
Posts: 1,831
Received Thanks: 786
The Minmax-Algorithm could be usefull..
.SkyneT. is offline  
Reply


Similar Threads Similar Threads
Adobe Flash Player Player Problem (Viedeos öffnen sich nicht)
05/22/2013 - Technical Support - 6 Replies
Hallo, Ich breuchte mal hilfe. Ich kann seit 3-4 Tagen keine Viedeos in Facebook,twitch und und ... Habs mir 10xmal runtergeladen und 10x installiert noch immer kann ich keine viedes anschauen. Nur in YouTube klappt alles. Mein letzter Download war Adblocker. Den ich jetzt auch deinstalliert habe. Aber es passiert immer noch nichts. Mein InternetBrowser Ist Explorer
[Problem]Player->Player Da wird nichts Angezeigt!!!!!!!!!!!!!
10/02/2010 - Metin2 Private Server - 6 Replies
Hey Leute, In NaviCat wen ich rein gehe und unter Player->Player gehe wird mir nichts Angezeigt überhaupt nichts. Jetzt meine frage wie kann ich das wieder Reparieren ???? Oder besser wie kann ich alle acc die drauf sind wieder ganz schnell Löschen und das dann wieder die Player->Player DB wieder geht ????? Egal wie es klappen könnte pls sagt es mirr ist wichtige gebe auch BIGG THXXX



All times are GMT +1. The time now is 01:01.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.