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

Click For Summary

Discussion Overview

The discussion revolves around determining the winning lines in a tic-tac-toe game using C programming. Participants explore various algorithms and approaches to identify winning lines based on the game's rules, considering both completed and potential winning conditions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant outlines the concept of winning lines, noting that a player wins by placing three marks in a row, column, or diagonal, and discusses the maximum possible winning lines based on board configurations.
  • Another participant suggests an implementation using an integer-based approach to check for winning conditions, defining specific values for 'O' and 'X' and listing the eight possible winning combinations.
  • A different approach is proposed where the board is initialized with zeros, and values are assigned based on player marks, with sums of rows, columns, and diagonals determining the winner.
  • One participant challenges the interpretations of winning lines, emphasizing that lines with empty fields should also be counted as winning lines, which leads to a reconsideration of previous responses.
  • A later reply acknowledges the clarification regarding the definition of winning lines and expresses a need for further thought on the topic.
  • A participant indicates they will pause their engagement with the topic due to other academic commitments.

Areas of Agreement / Disagreement

There is no consensus on the best approach to determine winning lines, as multiple competing methods and interpretations of the problem are presented. Participants express differing views on the definition of winning lines and the implications for their proposed solutions.

Contextual Notes

Participants highlight the importance of understanding the definition of winning lines, which includes lines that are not blocked by the opponent, leading to potential confusion in the implementation of solutions.

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 (occurrence: 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 ·
Replies
1
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
10K
  • · Replies 3 ·
Replies
3
Views
2K