- #1
galaxy_twirl
- 137
- 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
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.
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:
May I have some help please? Thank you! :)
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
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
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: