mardi 28 mars 2017

error expected primary-expression before '.' token

I'm currently trying to create a simple game of two player tic-tac-toe in c++

at the very end of the main class when I do my validation checking to see if the game has been won I get the exact error in the title ([ERROR]expected primary-expression before '.' token)... I'm fairly new to c++ and my "game_won" formula worked previously in python.... not sure if what I'm doing is strictly illegal or if it is just a syntax error. Any and all advice would be appreciated. (Friendly note that the game is no where near finished... I just want it to run with what I have currently) Thanks again.

code looks like this:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <limits>
#include <vector>
#include <assert.h>

using namespace std;

class Move{ 
  private:
    char piece = ' ';
  public:
    char set_piece(char new_piece) {
      piece = new_piece;
    }
    char get_piece() const {
      return piece;
    }

    Move(char new_piece) {
      piece = new_piece;
    }

    Move() {
      piece = ' ';
    }
};


class Grid {
  private:
    static const int SIZE = 3;
    Move ttt[SIZE][SIZE];
  public:
    Grid() {
      Move default_move(' ');
      for (int row=0; row < SIZE; ++row) {
        for (int col=0; col < SIZE; ++col) {
         ttt[row][col]=default_move;
        }
      }
    }

    string show() const { 
      string Grid_string;
      for (int row=0; row < SIZE; ++row) {
        for (int col=0; col < SIZE; ++col) {
         Grid_string+='|';
          Grid_string+=ttt[row][col].get_piece();
        }
        Grid_string+="|\n";
      }
      Grid_string+='\n';
      return Grid_string;
    }


    bool make_move(int row, int col, Move myMove) {

         if (row < 1 or row > SIZE or
         col < 1 or col > SIZE) {
         return false;
      } else {
         ttt[row-1][col-1]=myMove;
         return true;
       }
    }
};

int main(){
    cout<<"Welcome to tic-tac-toe! \n";
    Grid game1;
    cout<<game1.show();

    Move playerX('X');

    Move playerO('O');

    bool Game_Won = false;
    bool PlayerXwent = false;
    bool PlayerOwent = false;

    while(Game_Won != true){
        while(PlayerXwent != true){
            cout<<"Player X make a move; choose row and coulmn... \n";
            int row = 0;
            int col = 0;
            game1.make_move(1 ,1 , playerX);
            bool PlayerXWent = true;
        }

        cout<< game1.show();

        while(PlayerOwent != true){
            cout<<"Player O make a move; choose row and coulmn... \n";
            game1.make_move(2 ,2 , playerO);
            bool PlayerOWent = true;
        }

        cout<<game1.show();

    if (Move.get_piece(1,1) == Move.get_piece(1,2) ==      Move.get_piece(1,3)){
        Game_Won = true;
    }
    else if (Move.get_piece(2,1) == Move.get_piece(2,2) == Move.get_piece(2,3)){
        Game_Won = true;
        cout<<"game is won\n";
    }
    else if (Move.get_piece(3,1) == Move.get_piece(3,2) == Move.get_piece(3,3)){
        Game_Won = true;
        cout<<"game is won\n";
    }
    else if (Move.get_piece(1,1) == Move.get_piece(1,2) == Move.get_piece(1,3)){
        Game_Won = true;
        cout<<"game is won\n";
    }
    else if (Move.get_piece(2,1) == Move.get_piece(2,2) == Move.get_piece(2,3)){
        Game_Won = true;
        cout<<"game is won\n";
    }
    else if (Move.get_piece(3,1) == Move.get_piece(3,2) == Move.get_piece(3,3)){
        Game_Won = true;
        cout<<"game is won\n";
    }
    else if (Move.get_piece(1,1) == Move.get_piece(2,2) == Move.get_piece(3,3)){
        Game_Won = true;
        cout<<"game is won\n";
    }
    else if (Move.get_piece(3,1) == Move.get_piece(2,2) == Move.get_piece(1,3)){
        Game_Won = true;
        cout<<"game is won\n";
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire