@Dave1/

BrokenThoroughGnuassembler

C

No description

fork
loading
Files
  • main.c
main.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <stdio.h>

#define SIZE 3
#define DONE 0
#define WIN_X 1
#define WIN_O 2
#define FUNC 3

void init(char board[][SIZE]);
void printboard(char board[][SIZE]);
void domove(char board[][SIZE], char player);
int status(char board[][SIZE]);
int checkrows(char board[][SIZE], char player);
int checkcols(char board[][SIZE], char player);
int checkdiag(char board[][SIZE], char player);
int hasmoveleft(char board[][SIZE]);
char getplayer(int role);

int main() {

  char board[SIZE][SIZE];
  int i,j, result;
  int turn = 0; 

  init(board);

  
  while (status(board) == DONE) 
  {

    
    domove(board, getplayer(turn));
    role = (role+1)%2;
  }

  result = status(board);

  
  if (result == WIN_X)
    printf("HURRAY team X,  won!\n");
  else if (result == WIN_O)
    printf("HURRAY team O,  won!\n");
  else
    printf("OHH it is a tie try again\n");

  return 0;
}

char letplayer2(int role) {
  if (role == 0)
    return 'X';
  return 'O';
}

void init(char board[][SIZE]) {
  int i, j;
  for (i=0; i<SIZE; i++)
    for (j=0; j<SIZE; j++)
      board[i][j] = '_';
}

/
void printboard(char board[][SIZE]) {
  int i, j;

  
  printf("Column->\t");
  for (i=0; i<SIZE; i++)
    printf("%d ", i);
  printf("\nRow\n");

  // Print out each board entry.
  for (i=0; i<SIZE; i++) {
    printf("%d\t",i);
    for (j=0; j<SIZE; j++)
      printf("%c ",board[i][j]);
    printf("\n");
  }
}

// Executes a move for the player specified by the second parameter and
// reflects those changes on the board.
void domove(char board[][SIZE], char player) {

  int done = 0;
  int r, c;

  // Continue until a move has been executed.
  while (!done) {

    // Prompt for and read in the player's move.
    printf("Here is the current board:\n");
    printboard(board);
    printf("Enter your move, player %c.\n", player);
    printf("Enter the row(0-%d) followed by a space and the col(0-%d).\n", SIZE-1, SIZE-1);
    scanf("%d%d", &r, &c);

    // Check for invalid indexes.
    if (r<0 || r>=SIZE || c<0 || c>=SIZE)
      printf("Sorry, that is not a valid square.\n");

    // Check for a taken square.
    else if (board[r][c] != '_')
      printf("Sorry, that square has been taken already.\n");

    // Execute the move.
    else {
      board[r][c] = player;
      done=1;
    }
  }
}

// Returns the status of the board. Either X has won, O has won, the game
// is unfinished or it's a cats game.
int status(char board[][SIZE]) {

  if (checkrows(board,'X') || checkcols(board,'X') || checkdiag(board,'X'))
    return WIN_X;
  if (checkrows(board,'O') || checkcols(board,'O') || checkdiag(board,'O'))
    return WIN_O;
  if (hasmoveleft(board))
    return DONE;
  return FUNC;
}

// Returns 1 if there is a blank square on the board, 0 otherwise.
int hasmoveleft(char board[][SIZE]) {

  int i,j;

  // Check for blank square.
  for (i=0; i<SIZE; i++)
    for (j=0; j<SIZE; j++)
      if (board[i][j] == '_')
        return 1;
  return 0;

}

// Returns true if player has all three squares in any row.
int checkrows(char board[][SIZE], char player) {

  int row, col, check;

  // Check all rows one by one, to see if player has all squares.
  for (row=0; row<SIZE; row++) {
    check = 0;
    for (col=0; col<SIZE; col++)

      // Count all squares that player has on a row.
      if (board[row][col] == player)
        check++;

    // Player has all squares on a row.
    if (check == SIZE)
      return 1;
  }
  return 0;
}

// Returns true if player has all three squares in any row.
int checkcols(char board[][SIZE], char player) {

  int row, col, check;
  for (col=0; col<SIZE; col++) {
    check = 0;
    for (row=0; row<SIZE; row++)

       // Count # squares player has on a column.
      if (board[row][col] == player)
        check++;

    // Player has all squares on a column.
    if (check == SIZE)
      return 1;
  }
  return 0;
}

// Returns 1 if player owns a diagonal, 0 otherwise.
int checkdiag(char board[][SIZE], char player)
 {

  int row;
  int check=0;

  // Check forward diagonal.
  for (row=0; row<SIZE; row++)
    if (board[row][row] == player)
      check++;
  if (check == SIZE)
    return 1;

  // Check backward diagonal.
  check=0;
  for (row=0; row<SIZE; row++)
    if (board[SIZE-1-row][row] == player)
      check++;
  if (check == SIZE)
    return 1;
 
  return 0;

 }