Thread: checking for invalid move

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    6

    checking for invalid move

    I don't understand what he's doing for an invalid tic-tac-toe move at the end.
    It is not working. I get "Invalid move" but I cannot enter a new move.
    Why "getch()" ?
    What is he trying to do ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    char square[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    
    int choice, player;
    
    int checkForWin();
    void displayBoard();
    void markBoard(char mark);
    
    int main()
    {
      int gameStatus;
    
      char mark;
    
      player = 1;
    
      do {
        displayBoard();
    
        // change turns
        player = (player % 2) ? 1 : 2;
    
        // get input
        printf("Player %d, enter a number: ", player);
        scanf("%d", &choice);
    
        // set the correct character based on player turn
        mark = (player == 1) ? 'X' : 'O';
    
        // set board based on user choice or invalid choice
        markBoard(mark);
    
        gameStatus = checkForWin();
    
        player++;
    
      } while (gameStatus == -1);
    
      if (gameStatus == 1)
        printf("==>\aPlayer %d win ", --player);
      else
        printf("==>\aGame draw");
    
      return 0;
    }
    
    /*********************************************
    FUNCTION TO RETURN GAME STATUS
    1 FOR GAME IS OVER WITH RESULT
    -1 FOR GAME IS IN PROGRESS
    O GAME IS OVER AND NO RESULT
     **********************************************/
    int checkForWin()
    {
      int returnValue = 0;
    
      if (square[1] == square[2] && square[2] == square[3]) {
        returnValue = 1;
      } else if (square[4] == square[5] && square[5] == square[6])
        returnValue = 1;
    
      else if (square[7] == square[8] && square[8] == square[9])
        returnValue = 1;
    
      else if (square[1] == square[4] && square[4] == square[7])
        returnValue = 1;
    
      else if (square[2] == square[5] && square[5] == square[8])
        returnValue = 1;
    
      else if (square[3] == square[6] && square[6] == square[9])
        returnValue = 1;
    
      else if (square[1] == square[5] && square[5] == square[9])
        returnValue = 1;
    
      else if (square[3] == square[5] && square[5] == square[7])
        returnValue = 1;
    
      else if (square[1] != '1' && square[2] != '2' && square[3] != '3' &&
               square[4] != '4' && square[5] != '5' && square[6] != '6'
               && square[7] != '7' && square[8] != '8' && square[9] != '9')
        returnValue = 0;
      else
        returnValue = -1;
    
      return returnValue;
    }
    
    /*******************************************************************
    FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
     *******************************************************************/
    void displayBoard()
    {
      system("cls");                // clear screen, de stdlib.h
    
      printf("\n\n\tTic Tac Toe\n\n");
    
      printf("Player 1 (X)  -  Player 2 (O)\n\n\n");
    
      printf("     |     |     \n");
      printf("  %c  |  %c  |  %c \n", square[1], square[2], square[3]);
    
      printf("_____|_____|_____\n");
      printf("     |     |     \n");
    
      printf("  %c  |  %c  |  %c \n", square[4], square[5], square[6]);
    
      printf("_____|_____|_____\n");
      printf("     |     |     \n");
    
      printf("  %c  |  %c  |  %c \n", square[7], square[8], square[9]);
    
      printf("     |     |     \n\n");
    }
    
    /***************************************
    set the board with the correct character,
    x or o in the correct spot in the array
    ****************************************/
    void markBoard(char mark)
    {
      if (choice == 1 && square[1] == '1')
        square[1] = mark;
    
      else if (choice == 2 && square[2] == '2')
        square[2] = mark;
    
      else if (choice == 3 && square[3] == '3')
        square[3] = mark;
    
      else if (choice == 4 && square[4] == '4')
        square[4] = mark;
    
      else if (choice == 5 && square[5] == '5')
        square[5] = mark;
    
      else if (choice == 6 && square[6] == '6')
        square[6] = mark;
    
      else if (choice == 7 && square[7] == '7')
        square[7] = mark;
    
      else if (choice == 8 && square[8] == '8')
        square[8] = mark;
    
      else if (choice == 9 && square[9] == '9')
        square[9] = mark;
      else {
        printf("Invalid move ");
    
        player--;
        getch();                    // conio.h
      }
    }
    Last edited by Salem; 02-09-2020 at 10:09 PM. Reason: Removed Crayola eyebleed colours and fonts

  2. #2
    Registered User
    Join Date
    Feb 2020
    Posts
    6
    I see that my problem happens when I select a letter instead of a number.

    When I do that, the next "scanf("%d", &choice);" does not prompts the user and gets the last value used.

    Seems that scanf accepts the character from getch without prompting the user a second time ?
    Last edited by trogne; 02-09-2020 at 04:00 PM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by trogne View Post
    I see that my problem happens when I select a letter instead of a number.

    When I do that, the next "scanf("%d", &choice);" does not prompts the user and gets the last value used.

    Seems that scanf accepts the character from getch without prompting the user a second time ?
    What happens is that the standard input stream enters a failed state. You need to clear the state and remove the bad input.

    Instead of this, another option when dealing with such line-based input to read the entire line into a string then parse the string. This way, if the input is invalid, you just discard the string and move on to read the next line entered.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2020
    Posts
    6
    thank you !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 12-08-2019, 05:51 PM
  2. Checking for Invalid Input...Not Working?
    By eeengenious in forum C Programming
    Replies: 23
    Last Post: 02-23-2011, 09:23 AM
  3. Valid move checking in chess
    By transmitt in forum Game Programming
    Replies: 6
    Last Post: 09-09-2002, 08:12 PM
  4. When to move on?
    By SushiFugu in forum C++ Programming
    Replies: 5
    Last Post: 01-03-2002, 09:05 PM
  5. want to move bmp
    By b_amit4u in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 03:40 AM

Tags for this Thread