IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage C++ Discussion :

Tic Tac Toe : POO


Sujet :

Langage C++

  1. #1
    Nouveau membre du Club
    Profil pro
    Étudiant
    Inscrit en
    Septembre 2006
    Messages
    53
    Détails du profil
    Informations personnelles :
    Âge : 35
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2006
    Messages : 53
    Points : 26
    Points
    26
    Par défaut Tic Tac Toe : POO
    Bonjour,

    J'ai programmé un tic tac toe (morpion) en programmation objet.
    Il n'y a pas encore d'intelligence pour les joueurs. Ils mettent leurs pions au hasard.

    Le problème est que le jeu reste vide...

    Cela doit être un problème lié aux objets mais je débute en c++ et je le trouve pas.

    Voici mon code

    le header :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
     
    #ifndef TICTACTOE_H_
    #define TICTACTOE_H_
    // enum-Typ pour les deux joueurs
    enum player {noone, player1, player2};
     
    // classe board
    class Board {
    protected:
    	player board[3][3];
    	static const char symbol[3];
    	void drawRow(unsigned int i) const;
    public:
    	Board();
    	void print() const;
    	int getVal(unsigned int r, unsigned int c) const;
    	bool setVal(unsigned int r, unsigned int c, player val);
    	player getWinner() const;
    };
     
    const char Board::symbol[3] = {' ', 'X', 'O'};
     
    // classe player
    class Player {
    private:
    	player p;
    	int number;
    public:
    	Player(player playerNumber);
    	virtual ~Player() {};
    	void makeMove(Board &b);
    };
     
    // classe game
    class Game {
    protected:
    	Player *p1, *p2;
    	Board b;
    public:
    	Game(Player &p1, Player &p2);
    	Player* play();
    };
    #endif

    Le fichier cpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
     
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using std::cout;
    using std::endl;
    using std::cin;
     
    #include "TicTacToeOO.h"
     
    // constructeur de board
    Board::Board()
    {
    	for(int i = 0; i < 3; i++) {
    		for(int j = 0; j < 3; j++) {
    			board[i][j] = noone;
    		}
    	}
    }
     
     
    // affiche les colones (private)
    void Board::drawRow(unsigned int i) const
    {	
    	cout << " " << symbol[board[i][0]]
    	<< " | " << symbol[board[i][1]]
    	<< " | " << symbol[board[i][2]]
    	<< endl;
    }
     
     
    // print: affiche la board
    //      |   |
    //   ---|---|---
    //    X |   | O
    //   ---|---|---
    //    X | O |
    void Board::print() const
    {
    	drawRow(0);			// ligne du haut
    	cout << "---|---|---" << endl;
    	drawRow(1);			// ligne du milieu
    	cout << "---|---|---" << endl;
    	drawRow(2);			// ligne du bas
    	cout << endl << endl;
    }
     
    // retourne la valeur d'un champ
    int Board::getVal(unsigned int r, unsigned int c) const
    {
    	if(r>2 || c>2 || c<0 || r<0)
    	{
    		return -1;
    	}
    	else return board[r][c];
    }
     
    // inscrit une valeur dans un champ
    bool Board::setVal(unsigned int r, unsigned int c, player val)
    {
    	if(getVal(r,c)==0)
    	{
    		board[r][c]=val;
    		return true;
    	}
    	else return false;
    }
     
    // hasWon: controle qui a gagné
    player Board::getWinner() const
    {
     
    	int i = 0;
     
    	while(i < 3) {
     
    		if(board[0][i] == board[1][i] == board[2][i]) {
     
    			return board[0][i];
     
    		} else if(board[i][0] == board[i][1] == board[i][2]) {
     
    			return board[i][0];
     
    		}
     
    		i++;
     
    	}
     
    	if(board[0][0] == board[1][1] == board[2][2]) {
     
    		return board[0][0];
     
    	} else if(board[0][2] == board[1][1] == board[2][0]) {
     
    		return board[0][2];
     
    	}
     
    	return noone;
    }
     
     
    // constructeur de player
    Player::Player(player playerNumber)
    {
    	if(playerNumber==1)
    	{
    		p=player1;
    	}
    	else if(playerNumber==2)
    	{
    		p=player2;
    	}
    	else p=noone;
    }
     
    // makeMove: fait jouer un coup à un joueur
    void Player::makeMove(Board &b)
    {
    	int r,c;
     
    	do {
     
    		r = rand() % 3;
     
    		c = rand() % 3;
     
    	} while(b.getVal(r,c)!= 0);
     
    	b.setVal(r,c,p);
    }
     
    // constructeur game
    Game::Game(Player &pl1, Player &pl2)
    {
    	p1=&pl1;
    	p2=&pl2;
    }
    // play: chaque joueur joue
    Player* Game::play()
    {
    	p1->makeMove(b);
    	p2->makeMove(b);
    	if((b.getWinner())==noone) return NULL;
    	if(b.getWinner()==player1) return p1;
    	if(b.getWinner()==player2); return p2;	
    }
     
     
    int main()
    {
    	// initialisation variables
    	srand(time(NULL));
     
    	Board myboard;
     
    	Player play0=Player(noone);
    	Player play1=Player(player1);
    	Player play2=Player(player2);
     
    	Game mygame=Game(play1, play2);
     
    	myboard.print();
     
    	// joue
    	int i=0;
    	while(i<9)
    	{
    		Player *mywin=mygame.play();
    		if(mywin==&play1)
    		{
    			myboard.print();
    			cout<<"player1 has won..."<<endl;
    			return 0;
    		}
    		else if(mywin==&play2)
    		{
    			myboard.print();
    			cout<<"player1 has won..."<<endl;
    			return 0;
    		}
    		else
    		{
    			myboard.print();
    		}
    		i++;
    	}
    	return 0;
    }
    Le problème est peut être dans l'énumérations.

    Merci pour vos conseils.

  2. #2
    Membre expérimenté
    Avatar de coyotte507
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    1 327
    Détails du profil
    Informations personnelles :
    Âge : 34
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 1 327
    Points : 1 452
    Points
    1 452
    Par défaut
    c'est normal ton myboard n'est pas le même que celui de mygame.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    int main()
    {
    	// initialisation variables
    	srand(time(NULL));
     
    	Player play0=Player(noone);
    	Player play1=Player(player1);
    	Player play2=Player(player2);
     
    	Game mygame=Game(play1, play2);
     
    	mygame.print();
     
    	// joue
    	int i=0;
    	while(i<9)
    	{
    		Player *mywin=mygame.play();
    		if(mywin==&play1)
    		{
    			mygame.print();
    			cout<<"player1 has won..."<<endl;
    			return 0;
    		}
    		else if(mywin==&play2)
    		{
    			mygame.print();
    			cout<<"player1 has won..."<<endl;
    			return 0;
    		}
    		else
    		{
    			mygame.print();
    		}
    		i++;
    	}
    	return 0;
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    // classe game
    class Game {
    protected:
    	Player *p1, *p2;
    	Board b;
    public:
    	Game(Player &p1, Player &p2);
    	Player* play();
    	void print(){b.print();}
    };
    Voilà les modifs que j'ai faites.

  3. #3
    Expert confirmé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Décembre 2003
    Messages
    3 549
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Décembre 2003
    Messages : 3 549
    Points : 4 625
    Points
    4 625
    Par défaut
    En quoi c'est orienté objet ?

Discussions similaires

  1. Tic tac toe- déterminer une position gagnante
    Par shirya dans le forum Intelligence artificielle
    Réponses: 7
    Dernier message: 26/10/2010, 10h33
  2. Réponses: 23
    Dernier message: 06/12/2008, 19h40
  3. algorithmes pour morpion/tic tac toe
    Par shirya dans le forum Algorithmes et structures de données
    Réponses: 4
    Dernier message: 17/01/2008, 02h35
  4. Tic tac toe
    Par Invité(e) dans le forum Prolog
    Réponses: 9
    Dernier message: 17/05/2005, 23h08

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo