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

AI Thread Summary
The discussion focuses on creating a C program to determine winning lines in a tic-tac-toe game, defined as rows, columns, or diagonals that are not blocked by the opponent. Participants share sample board inputs and expected outputs, highlighting the need to account for both players' potential winning lines. Key points include the maximum possible winning lines and the importance of unblocked spaces in determining these lines. There is also a clarification that winning lines can exist even if the game is not concluded. The conversation emphasizes the need for a clear understanding of the winning line definition to implement the algorithm correctly.
galaxy_twirl
Messages
137
Reaction score
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:
#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:
Physics news on Phys.org
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:
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.
 
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.
 
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.
 
Borek said:
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.
You're right. I missed this definition in the OP:
galaxy_twirl said:
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.
I'll have to give this some more thought.
 
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.
 

Similar threads

Replies
1
Views
1K
Replies
3
Views
2K
Replies
6
Views
5K
Replies
11
Views
9K
Replies
3
Views
2K
Back
Top