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

• galaxy_twirl
In summary, the conversation is about finding the winning lines in a game of tic-tac-toe played by two players, x and o. There are eight different possibilities for a win in a tic-tac-toe game and the winning line is defined as a line (row, column or diagonal) that is not blocked by the opponent. Various solutions were suggested, including an implementation using integers and one using chars or bools. It was highlighted that in this game, lines with empty fields still count as winning lines for both players, even if the game has not yet ended. The OP thanked everyone for their responses and mentioned that they will come back to this question at a later time.
galaxy_twirl
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:
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.

## 1. How do I create a winning line for tic-tac-toe in C programming?

To create a winning line for tic-tac-toe in C programming, you will need to use a multidimensional array to represent the game board. Then, you can use conditional statements to check for winning combinations in the array, such as three in a row, column, or diagonal. You will also need to keep track of player moves and update the array accordingly.

## 2. How can I prevent the computer from cheating in my tic-tac-toe game?

In order to prevent the computer from cheating in a tic-tac-toe game, you can implement a strategy where the computer makes random moves until it detects a potential winning line for the player. This will make the game more challenging and less predictable for both players.

## 3. Can I use a different data structure instead of a multidimensional array for the game board?

Yes, you can use a different data structure instead of a multidimensional array for the game board. Some other options include using a linked list or a binary tree. However, an array is typically the most efficient and straightforward choice for representing a tic-tac-toe game board in C programming.

## 4. How can I improve the game logic for my tic-tac-toe program?

To improve the game logic for a tic-tac-toe program, you could implement a recursive algorithm that analyzes all possible moves and outcomes to make the best move for the computer player. This will make the game more challenging and strategic for players.

## 5. How do I handle a tie game in my tic-tac-toe program?

To handle a tie game in a tic-tac-toe program, you can add a condition to check for a full game board with no winning combinations. If this condition is met, you can display a message to the players that the game has ended in a tie. Additionally, you could give players the option to play again or end the game.

• Engineering and Comp Sci Homework Help
Replies
1
Views
872
• Engineering and Comp Sci Homework Help
Replies
10
Views
1K
• Engineering and Comp Sci Homework Help
Replies
8
Views
1K
• Engineering and Comp Sci Homework Help
Replies
3
Views
1K
• Engineering and Comp Sci Homework Help
Replies
6
Views
4K
• Engineering and Comp Sci Homework Help
Replies
4
Views
2K
• Engineering and Comp Sci Homework Help
Replies
3
Views
4K
• Engineering and Comp Sci Homework Help
Replies
1
Views
1K
• Engineering and Comp Sci Homework Help
Replies
2
Views
2K
• Engineering and Comp Sci Homework Help
Replies
3
Views
2K