Tic-Tac-Toe

Completed game logic, lost in errors

Page 1 of 1

12 Replies - 2327 Views - Last Post: 05 May 2010 - 01:08 PM Rate Topic: -----

#1 RGLAsnakeMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 05-March 10

Tic-Tac-Toe

Posted 05 May 2010 - 08:37 AM

Something is wrong ... a tad frustrated
getting a compile error on line 24.

#include<iostream>
#include<string>
#include<vector>
#include <algorithm>

using namespace std;

//global constants
const char X = 'X';
const char O = 'O';
const char EMPTY = ' ';
const char TIE = 'T';
const char NO_ONE = 'N';

//function prototypes
void instructions();
char askYesNo(string question);
int askNumber(string question, int high, int low = 0);
char humanPiece();
char opponent(char piece);
void displayBoard(const vector<char>& board);
char winner(const vector<char>& board);
bool isLegal(const vector<char>& board, int move);
int humanMove(const vector<char>& board, char human);
int computerMove(vector<char> board, char computer);
void announceWinner(char winner, char computer, char human);

//main function
int main(){
      
      int move;
      const int NUM_SQUARES = 9;
      vector<char> board(NUM_SQUARES, EMPTY);
      
      instructions();
      char human = humanPiece();
      char computer = opponent(human);
      char turn = X;
      displayBoard(board);
      
      while (winner(board) == NO_ONE) {
      
            if (turn == human) {
                  move = humanMove(board);
                  board[move] = human;
            }
            
            else {
                  move = computerMove(board, computer);
                  board[move] = computer;
            }
            displayBoard(board);
            turn = opponent(turn);
      }
      
      announeWinner(winner(board), computer human);
      
      return 0;
}

//displays game instructions and gives the computer's opponent some attitude
void instructions(){
      cout << "Welcome to the extreme man v. machine throwdown: Tic-Tac-Toe.\nWhere human brain is pit against silicon processor\n\n";
      cout << "Make your move by entering a number, 0-8. The number\ncorresponds to the desired board position, as illustrated:\n\n";
      
      cout << "        0 | 1 | 2\n";
      cout << "        --------\n";
      cout << "        3 | 4 | 5\n";
      cout << "        --------\n";
      cout << "        6 | 7 | 8\n\n";
      
      cout << "Prepare yourself, human. The battle is about to begin\n\n";
}

//asks a yes or no question. keeps asking the question until the player enters either a y or an n. it recieves a question and returns either a 'y' or an 'n'.
char askYesNo(string question){
      char response;
      do {
            cout << question << " (y/n): ";
            cin >> response;
            } 
      while (response != 'y' && response != 'n');
      
      return response;
}

//asks for a number within a range and keeps asking until the player enters a valid number. it recievesd a question, a high number, and a low number. it returns a number within the range specified
int askNumber(string question, int high, int low){
      int number;
      do {
            cout << question << " (" << low << " - " << high << "): ";
            cin >> number;
           }
      while (number > high || number < low);
      
      return number;
}

//asks player if he wants to go first, and returns the humans piece based on that choice . . . Tic-Tac-Toe tradition, X goes first
char humanPiece() {
      char go_first = askYesNo("Do you require the first move?");
      if (go_furst == 'y') {
            cout << "\nThen take the first move. You will need it.\n";
            return X;
      }
      else {
            cout << "\nYour bravery will be your undoing ... I will go first.\n"
            return O;
      }
      
//gets a piece ('X' or 'O') and returns the opponents piece ('X' or 'O')
char opponent(char piece) {
      if (piece == X)
            return O;
      else
            return X;
}


void displayBoard(const vector<char>& board)
{
cout << "\n\t" << board[0] << " | " << board[1] << " | " << board[2];
cout << "\n\t" << "————-";
cout << "\n\t" << board[3] << " | " << board[4] << " | " << board[5];
cout << "\n\t" << "————-";
cout << "\n\t" << board[6] << " | " << board[7] << " | " << board[8];
cout << "\n\n";
}

char winner(const vector<char>& board) {
// all possible winning rows
const int WINNING_ROWS[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} };


const int TOTAL_ROWS = 8;
// if any winning row has three values that are the same (and not EMPTY),
// then we have a winner
for(int row = 0; row < TOTAL_ROWS; ++row)
{
if ( (board[WINNING_ROWS[row][0]] != EMPTY) &&
(board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]]) &&
(board[WINNING_ROWS[row][1]] == board[WINNING_ROWS[row][2]]) )
{
return board[WINNING_ROWS[row][0]];
}
}

// since nobody has won, check for a tie (no empty squares left)
if (count(board.begin(), board.end(), EMPTY) == 0)
return TIE;

// since nobody has won and it isn’t a tie, the game ain’t over
return NO_ONE;
}

inline bool isLegal(int move, const vector<char>& board)
{
return (board[move] == EMPTY);
}

int humanMove(const vector<char>& board, char human)
{
int move = askNumber(“Where will you move?”, (board.size() - 1));
while (!isLegal(move, board))
{
cout << "\nThat square is already occupied, foolish human.\n";
move = askNumber("Where will you move?", (board.size() - 1));
}
cout << "Fine...\n";
return move;
}

{
cout << "I shall take square number: ";
// if computer can win on next move, make that move
for(int move = 0; move < board.size(); ++move)
{
if (isLegal(move, board))
{
board[move] = computer;
if (winner(board) == computer)
{
cout << move << endl;
return move;
}
// done checking this move, undo it
board[move] = EMPTY;
}
}

// if human can win on next move, block that move
char human = opponent(computer);
for(int move = 0; move < board.size(); ++move)
{
if (isLegal(move, board))
{
board[move] = human;
if (winner(board) == human)
{
cout << move << endl;
return move;
}
// done checking this move, undo it
board[move] = EMPTY;
}
}

// the best moves to make, in order
const int BEST_MOVES[] = {4, 0, 2, 6, 8, 1, 3, 5, 7};
// since no one can win on next move, pick best open square
for(int i = 0; i < board.size(); ++i) {
int move = BEST_MOVES[i];
if (isLegal(move, board)) {
cout << move << endl;
return move;
}
}
}

void announceWinner(char winner, char computer, char human)
{
if (winner == computer)
{
cout << winner << "‘s won!\n";
cout << "As I predicted, human, I am triumphant once more — proof\n";
cout << "that computers are superior to humans in all regards.\n";
}
else if (winner == human)
{
cout << winner << "‘s won!\n";
cout << "No, no! It cannot be! Somehow you tricked me, human.\n";
cout << "But never again! I, the computer, so swear it!\n";
}
else
{
cout << "It’s a tie.\n";
cout << "You were most lucky, human, and somehow managed to tie me.\n";
cout << "Celebrate, drink a beer... for this is the best you will ever achieve.\n";
}
}




This post has been edited by RGLAsnakeMan: 05 May 2010 - 08:38 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Tic-Tac-Toe

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Tic-Tac-Toe

Posted 05 May 2010 - 08:43 AM

Suppose you din't have an Internet connection. What would you do? How would you analyze the errors?
Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Tic-Tac-Toe

Posted 05 May 2010 - 08:46 AM

This will come as shocking news to you, I imagine, but we programmers are not computers. If you dump code on us, we have no way of instantly knowing what the error is.

Therefore, you need to copy-paste errors in your post.

Also, compile errors are a natural part of programming. Telling us you have one in your code is nothing special. If you cannot solve the mistake, you need to describe where you failed. That means describing how you attempted to solve the problem, the results of your efforts, and why you are stuck.
Was This Post Helpful? 0
  • +
  • -

#4 RGLAsnakeMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 05-March 10

Re: Tic-Tac-Toe

Posted 05 May 2010 - 08:47 AM

View Postsarmanu, on 05 May 2010 - 07:43 AM, said:

Suppose you din't have an Internet connection. What would you do? How would you analyze the errors?


Thanks sarmanu -- I appreciate your thoughts and I have tried to figure this out on my own, but I am a student with an Internet connection. The error is as follows and I can't figure out why it is telling me this:
24 too few arguments to function 'int humanMove(const std::vector<char, std::allocator<char> >&, char)'
Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Tic-Tac-Toe

Posted 05 May 2010 - 08:50 AM

Quote

The error is as follows and I can't figure out why it is telling me this:
What part of the error do you not understand?

EDIT: By the way, I just ran your code through a compiler. I don't get the error you claim to get. On the other hand, I get countless other errors you don't talk about.

An interesting disparity, to put it mildly.

This post has been edited by Oler1s: 05 May 2010 - 08:54 AM

Was This Post Helpful? 0
  • +
  • -

#6 RGLAsnakeMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 05-March 10

Re: Tic-Tac-Toe

Posted 05 May 2010 - 08:58 AM

View PostOler1s, on 05 May 2010 - 07:50 AM, said:

Quote

The error is as follows and I can't figure out why it is telling me this:
What part of the error do you not understand?


My apologies for having all this source code but seeming like a total newb for a compile error -- I don't understand why I am getting this error, all my arguments seem legit. What do you suggest I look for throughout my program to fix this error?; I can't swee anything wrong my perspective -- trying to get some different views. Ok? :oops:
Was This Post Helpful? 0
  • +
  • -

#7 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Tic-Tac-Toe

Posted 05 May 2010 - 09:00 AM

How many parameters does "humanMove" function takes?
Was This Post Helpful? 0
  • +
  • -

#8 RGLAsnakeMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 05-March 10

Re: Tic-Tac-Toe

Posted 05 May 2010 - 09:03 AM

View PostOler1s, on 05 May 2010 - 07:50 AM, said:

Quote

The error is as follows and I can't figure out why it is telling me this:
What part of the error do you not understand?

EDIT: By the way, I just ran your code through a compiler. I don't get the error you claim to get. On the other hand, I get countless other errors you don't talk about.

An interesting disparity, to put it mildly.


Thanks for that info -- I'm trying to be respectful here but it doesn't seem like I am getting much in return.
:mellow:

View Postsarmanu, on 05 May 2010 - 08:00 AM, said:

How many parameters does "humanMove" function takes?


2
Was This Post Helpful? 0
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Tic-Tac-Toe

Posted 05 May 2010 - 09:04 AM

Quote

My apologies for having all this source code but seeming like a total newb for a compile error
Ignorance is not the problem. Failure to go through problem solving is. The error is in English. Have you tried reading it?

Quote

I don't understand why I am getting this error, all my arguments seem legit.
Really? "too few arguments to function" What does that mean?

Quote

What do you suggest I look for throughout my program to fix this error?
The error. It's in English. It's telling you something. Look at the error please.

Again, I note that your code gives a different set of errors than the one you are asking about here. Looking at your code, I can confirm that you should not be getting the error you claim to have. How can we help you when the code and the problems you talk about, don't match?

EDIT:

Quote

Thanks for that info -- I'm trying to be respectful here but it doesn't seem like I am getting much in return.
No, I'm giving you the minimal amount you need to make progress.

If the problem is solving the error, then the minimal amount of help you need is guidance on how to go about solving it. Not the solution. Not fixed code. Guidance on how you personally can solve your own error on your own code.

This post has been edited by Oler1s: 05 May 2010 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#10 keithgarry  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 63
  • Joined: 26-August 09

Re: Tic-Tac-Toe

Posted 05 May 2010 - 09:20 AM

snake, their comments come off as rude but they all warrant the tone. You need to step back a few chapters. You're reading Beginning C++ by Dawson right? This book doesn't do so well to explain the inner workings of C++. It does give many good exercises *after* you get your foot in the door.

Personally I think you should get yourself another book, preferably one around a thousand pages or so and gradually warms you up. C++ Primer Plus is good, but any of those textbooks will do fine. The error that you copy/pasted has to do with passing arguments to functions. It's very clear to me and the others as to what is wrong. I strongly advise you to pull back before you lose too many of your men.
Was This Post Helpful? 0
  • +
  • -

#11 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Tic-Tac-Toe

Posted 05 May 2010 - 09:26 AM

Ah, I see. You have copy-pasted the code from here. And you want us to fix it for you?
Was This Post Helpful? 1
  • +
  • -

#12 keithgarry  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 63
  • Joined: 26-August 09

Re: Tic-Tac-Toe

Posted 05 May 2010 - 09:36 AM

sarmanu, finishing the last of his wooden voodoo dolls, sets them into the furnace. And a blank expression permeates his face as they all burn away.
Was This Post Helpful? 0
  • +
  • -

#13 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,460
  • Joined: 20-March 10

Re: Tic-Tac-Toe

Posted 05 May 2010 - 01:08 PM

I can get the program to compile and run error free

Its got about 10-12 errors in it
including missing lines.

If you cant fix the first error your not going to be able to fix the rest of the program.

Just giving you a heads up dude.

You might want to give up on this and start something simpler.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1