1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Winning Lines for Tic-Tac-Toe (C programming)

  1. Oct 28, 2014 #1
    The Question

    Winning Lines

    Tic-tac-toe is a game played by two players o and x. A player wins when he/she succeeds in placing three respective marks in a horizontal, vertical or diagonal row.

    Given a 3-by-3 tic-tac-toe board, a winning line is a line (row, column or diagonal) that is not blocked by the opponent. For example, given the sample board below:

    xox
    .o.
    ..x

    • player x has three winning lines --- first and last column, as well as the last row
    • player o has two winning lines --- the middle row and middle column
    Write a program that reads a 3-by-3 tic-tac-toe board and determines the winning lines of both players. Assume that the board represents a valid play.

    Sample runs:

    The following are sample runs of the program. User input is underlined. Ensure that the last line of output is followed by a newline character.
    • Sample run #1:

      Enter board:
      xox
      .o.
      ..x

      x winning lines: 3
      o winning lines: 2
    • Sample run #2:

      Enter board:
      .ox
      .ox
      ..x

      x winning lines: 3
      o winning lines: 2
    • Sample run #3:

      Enter board:
      xox
      xoo
      oxx
    x winning lines: 0
    o winning lines: 0

    I ran into slight difficulties while trying to look for a pattern to obtain the algorithm for the above. However, I managed to come up with following:

    1. Max. possible number of winning lines: 4 (Occurence: When I have two of the same type (x or o) side-by-side in the middle of the tic-tac-toe sheet.

    2. If a symbol is in the middle box and its surrrounding (up, down, left, right) are clear, max winning lines = 3.

    3. If a symbol is at the four corners, and there are no opposite symbols on its vertical, lateral and diagonal sides, max winning lines = 3.

    4. General: If a symbol has "."s on its lateral, vertical or diagonal axes, it can form a winning line.

    I wrote the code below:

    Code (Text):


    #include <stdio.h>
    #define ROW 3
    #define COL 3

    void fillArray(char arr[][COL]);
    int numWin(char arr[][COL], int count);

    int main(void)
    {
        char arr[ROW][COL];
        int x=0, o=0;

        fillArray(arr[][COL]);
        numWin(arr[][COL], count);

        printf("x winning lines: %d\n", x);
        printf("o winning lines: %d\n", o);

        return 0;
    }

    void fillArray(char arr[][COL])
    {
         int i=0, j=0;

         for(i=0;i<ROW;i++)
         {
             for(j=0;j<COL;j++)
             {
                 scanf(" %c", &arr[i][j]);
             }
         }
    }

    int xWinline(char arr[][COL], int count) //Too tired to continue. :(
    {
        int i=0, j=0;
     
        for(i=0;i<ROW;i++)
        {
             for(j=0;j<COL;j++)
             {
                 if(char[i][j] == ".")
                     return 0;
             }
        }
     
    May I have some help please? Thank you! :)
     
    Last edited: Oct 28, 2014
  2. jcsd
  3. Oct 30, 2014 #2

    Zondrina

    User Avatar
    Homework Helper

    There are 8 different possibilities for a win in a tic-tac-toe game. You need to monitor the board separately for both players and return a win if one of the players satisfies one of the eight winning conditions.

    For example, using an ##int## based implementation that passes the game board in:

    Code (Text):

    int winCheck (const int gameBoard[]) {
      //8 Different possibilities for a win in a tic tac toe game.
      //== 10 means the spot is taken by 'O'.
      //== 11 means the spot is taken by 'X'
     
      if((gameBoard[0] == 10 && gameBoard[3] == 10 && gameBoard[6] == 10)
      || (gameBoard[1] == 10 && gameBoard[4] == 10 && gameBoard[7] == 10)
      || (gameBoard[2] == 10 && gameBoard[5] == 10 && gameBoard[8] == 10)
      || (gameBoard[0] == 10 && gameBoard[1] == 10 && gameBoard[2] == 10)
      || (gameBoard[3] == 10 && gameBoard[4] == 10 && gameBoard[5] == 10)
      || (gameBoard[6] == 10 && gameBoard[7] == 10 && gameBoard[8] == 10)
      || (gameBoard[0] == 10 && gameBoard[4] == 10 && gameBoard[8] == 10)
      || (gameBoard[2] == 10 && gameBoard[4] == 10 && gameBoard[6] == 10))
      return 0;
     
      else if((gameBoard[0] == 11 && gameBoard[3] == 11 && gameBoard[6] == 11)
      || (gameBoard[1] == 11 && gameBoard[4] == 11 && gameBoard[7] == 11)
      || (gameBoard[2] == 11 && gameBoard[5] == 11 && gameBoard[8] == 11)
      || (gameBoard[0] == 11 && gameBoard[1] == 11 && gameBoard[2] == 11)
      || (gameBoard[3] == 11 && gameBoard[4] == 11 && gameBoard[5] == 11)
      || (gameBoard[6] == 11 && gameBoard[7] == 11 && gameBoard[8] == 11)
      || (gameBoard[0] == 11 && gameBoard[4] == 11 && gameBoard[8] == 11)
      || (gameBoard[2] == 11 && gameBoard[4] == 11 && gameBoard[6] == 11))
      return 1;
     
      else
      return -1;
    }
     
    The code returns ##0## if 'O' is the winner, ##1## if 'X' is the winner, or ##-1## if no one has won the game yet.

    This of course is not the only implementation. You could use chars or bools if you prefer. The important thing is to understand how a win is achieved.
     
  4. Oct 30, 2014 #3

    Mark44

    Staff: Mentor

    A much simpler solution is to initialize all cells to 0, and fill the appropriate cells of a particular game with 1 in place of 'O', and -1 in place of 'X'. Then, after the game is laid out, add each of the three rows, three columns, and two diagonals. If a row, column, or diagonal adds to 3, the 'O' player wins. If a row, column, or diagonal adds to -3, the 'X' player wins. Any other combination results in a tie.

    I am assuming that no game would be input that had both the 'O' player and 'X' player as winners.
     
  5. Oct 30, 2014 #4

    Borek

    User Avatar

    Staff: Mentor

    Zondrina, Mark: perhaps I am missing something, but the definition of a winning line OP posted makes both your answers wrong. It is not about finding who won the game, as lines with empty fields still count as winning lines (... is a winning line for both players), even if the game has not yet ended.
     
  6. Oct 30, 2014 #5

    Mark44

    Staff: Mentor

    You're right. I missed this definition in the OP:
    I'll have to give this some more thought.
     
  7. Nov 1, 2014 #6
    Hi everyone. Thank you for replying to my question. :) I think I will leave this question for a while as I have to tend to my other subjects which I am also struggling in. >< Sorry. I will come to this soon~ Thank you all once again.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Winning Lines for Tic-Tac-Toe (C programming)
  1. Program in C (Replies: 1)

  2. C++ Program (Replies: 17)

Loading...