Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by Wrzask ( 13 years ago )
header:

enum SIGN { SI_CIRCLE='O', SI_CROSS='X' };
enum FIELD { F_EMPTY, 
             F_CIRCLE=SI_CIRCLE, 
             F_CROSS=SI_CROSS };
enum GAMESTATE { GM_NONSTART, GM_MOVE, GM_WON, GM_DRAW };

bool GameStart();
bool GameMove(unsigned i);
bool DrawGame();

//extern GAMESTATE StanGry;

aktuany main:

#include <conio.h>
#include <iostream>
#include <time.h>
#include "game.h"

using namespace std;

GAMESTATE StanGry=GM_NONSTART;
FIELD Plansza[3][3]={F_EMPTY, F_EMPTY, F_EMPTY,
                     F_EMPTY, F_EMPTY, F_EMPTY,
                     F_EMPTY, F_EMPTY, F_EMPTY};
SIGN AktualnyGracz;

bool GameStart()
     {
     if (StanGry!=GM_NONSTART) return false;
     
     srand (static_cast<unsigned>(time(NULL)));
     AktualnyGracz=(rand()% 2 == 0 ? SI_CIRCLE : SI_CROSS);
     StanGry=GM_MOVE;
     
     return true;
     }

bool GameMove(unsigned iPole)
     {
     if (StanGry!=GM_MOVE) return false;
     if (iPole>10 && iPole<1) return false;
     int y = (iPole-1)/3;
     int x = (iPole+2)%3;
     
     if (Plansza[y][x]!=F_EMPTY) return false;
     else  
     Plansza[y][x]=static_cast<FIELD>(AktualnyGracz);
     
     FIELD Warunki[8][3]={Plansza[0][0], Plansza[0][1], Plansza[0][2],
                          Plansza[1][0], Plansza[1][1], Plansza[0][2],
                          Plansza[2][0], Plansza[2][1], Plansza[1][2],
                          Plansza[0][0], Plansza[1][0], Plansza[2][0],
                          Plansza[0][1], Plansza[1][1], Plansza[2][1],
                          Plansza[0][2], Plansza[1][2], Plansza[2][2],
                          Plansza[0][0], Plansza[1][1], Plansza[2][2],
                          Plansza[0][2], Plansza[1][1], Plansza[2][0]};
                          
     int iIloscRund=0;
     int iIloscCircle=0,
         iIloscCross=0;   
                              
     for (int i=0;i<8;++i)
     {
         for (int j=0;j<3;++j)
         {
             
             if (Warunki[i][j]==F_CIRCLE)
             {             
              ++iIloscCircle;
              if (iIloscCircle==3)
               {StanGry=GM_WON;
                if(j==2) iIloscCircle=0;
                return true; 
               }
             }
             
             if (Warunki[i][j]==F_CROSS)
             {
              ++iIloscCross;
              if(iIloscCross==3)
               {StanGry=GM_WON;
                return true;             
               }
              if(j==2)iIloscCross=0;
             }
         }                     
     }
     
     for (int i=0;i<3;++i)
     {
         for (int j=0;j<3;++j)
         {
          if (Plansza[i][j]!=F_EMPTY) ++iIloscRund;
          if (iIloscRund==9)
          {
           StanGry=GM_DRAW;
           return true;
          }
         }
     }
     
     AktualnyGracz=(AktualnyGracz==SI_CIRCLE ? SI_CROSS : SI_CIRCLE);
     return true;
     }

bool DrawGame()
     {
     if (StanGry == GM_NONSTART) return false;
     system &#40;"cls"&#41;;
     
     cout<<"KOLKO I KRZYZYK"<<endl;
     cout<<"_______________";
     cout<<endl;
     
     
     for (int i=1;i<10;i++)
     {
         int y = (i-1)/3;
         int x = (i+2)%3;
         
         if ((i+2)%3==0) cout<<"     |";
         if (Plansza[y][x]==F_EMPTY)
         cout<<i;
         else
         cout<<static_cast<char>(Plansza[y][x]);
         if (i%3==0) {cout<<"|     "; cout<<endl;};
     }
      
     switch(StanGry)
     {
                     
      case GM_MOVE: cout<< "Podaj numer pola w ktorym chcesz postawic ",(AktualnyGracz==SI_CIRCLE ? "kolko" : "krzyzyk"), "."; break;
           
      case GM_WON: cout<< "Wygral gracz stawiajacy: ", (AktualnyGracz==SI_CIRCLE ? "kolko" : "krzyzyk"), "."; break;
           
      case GM_DRAW: cout<< "Gra zakonczona remisem."; break;
      
     }
                
     return true;
     
     }
     
int main()
{    

GameStart();
for (;;)
{
    DrawGame();
    
    if (StanGry == GM_MOVE)
    {
    unsigned iPole;
    std::cin >> iPole;
    GameMove (iPole);
    }
    else if (StanGry == GM_WON || StanGry == GM_DRAW)
    break;
}

getch();

return 0;
}

 

Revise this Paste

Your Name: Code Language: