-
Notifications
You must be signed in to change notification settings - Fork 0
/
winLose.c
executable file
·169 lines (142 loc) · 4.23 KB
/
winLose.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "winLose.h"
#include "boardWork.h"
#include <stdio.h>
#include <stdlib.h>
int gameOver(char** board, int numRows, int numCols, int playerChar, int numWin){
int rWin,cWin,dWin,rdWin = 0;
int numChar = 0;
int i,j = 0;
char checkPlayer = '.';
if(playerChar == 'X'){
checkPlayer = 'O';
}
else if(playerChar == 'O'){
checkPlayer = 'X';
}
//call functions to check if the latest move resulted in a win
rWin = rowWin(board, numRows, numCols, checkPlayer, numWin);
cWin = colWin(board, numRows, numCols, checkPlayer, numWin);
dWin = diagWin(board, numRows, numCols, numWin, checkPlayer);
rdWin = rightdiagWin(board, numRows, numCols, numWin, checkPlayer);
//iterate through board
//keep track of the number of characters on the board with the counter numChar
for(i = 0; i < numRows; i++){
for(j = 0; j < numCols; j++){
if((board[i][j] == 'X') || (board[i][j] == 'O')){
numChar++;
}
}
}
//if the latest play resulted in 0 values for every win scenario
// and the board is full
//call the isTie function
if((rWin == 0) && (cWin == 0) && (dWin == 0) && (rdWin == 0) && (numChar == numRows * numCols)){
isTie();
}
//if the latest play resulted in 1 value, the player won
if((rWin == 1) || (cWin == 1) || (dWin == 1) || (rdWin == 1)){
gameWon(checkPlayer);
}
return 0; //0 means no one won; play the game again
}
//tie game situation: numRows == numRows * numCols
void isTie(){
printf("Tie game!");
exit(0);
}
//declare the winner of th game
void gameWon(int checkPlayer){
int curPlayer;
//the latest player by the 'X' is coded as Player 1
if(checkPlayer == 'X'){
curPlayer = 1;
}
//the last play by the 'O' is coded as Player 2
if(checkPlayer == 'O'){
curPlayer = 2;
}
printf("Player %d won!", curPlayer);
exit(0);
}
int rowWin(char** board, int numRows, int numCols, char checkPlayer, int numWin){
int i,j = 0;
int charinRow = 0;
for(i = 0; i < numRows; i++){
charinRow = 0;
for(j = 0; j < numCols; j++){
if(board[i][j] == checkPlayer){
charinRow++;
}
}
if(charinRow == numWin){
return 1;
}
}
return 0; //return 1 if win
}
int colWin(char** board, int numRows, int numCols, int checkPlayer, int numWin){
int i,j = 0;
int charinCol = 0;
for(j = 0; j < numCols; j++){
charinCol = 0;
for(i = 0; i < numRows; i++){
if(board[i][j] == checkPlayer){
charinCol++;
}
}
if(charinCol == numWin){
return 1;
}
}
return 0; //return 1 if win
}
int diagWin(char** board, int numRows, int numCols, int numWin, char checkPlayer){
int i,j,l,m = 0;
int h = 0;
if((numCols >= numWin) || (numRows >= numWin)) {
while(m < numRows){
l = 0;
for(i = m; i >= 0; i--){ //FIXME - check on how to iterate diagonal
for(j = 0; ((j < numCols) && (j < m)); j++){
l++;
if(board[i][j] == checkPlayer){
h++;
}
}
}
m++;
}
if((l >= numWin) && (h == numWin)){
return 1;
}
}
else{
return 0;
}
return 1; //return 1 if win
}
int rightdiagWin(char** board, int numRows, int numCols, int numWin, char checkPlayer){
int i,j,l,m = 0;
int h = 0;
if((numCols >= numWin) || (numRows >= numWin)) {
while(m < numRows){
l = 0;
for(i = m; i >= 0; i--){ //FIXME - check on how to iterate right diagonal
for(j = numCols - 1; ((j < l) && (j < m)); j--){
l++;
if(board[i][j] == checkPlayer){
h++;
}
}
}
m++;
}
if((l >= numWin) && (h == numWin)){
return 1;
}
}
else{
return 0;
}
return 1; //return 1 if win
}