Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- #include <stdlib.h>
- #define SPACE ' '
- void disp_matrix(void);
- int check(void);
- void player(void);
- int pc(void);
- int ininum = 9;
- char done;
- char matrix[3][3] = {
- {SPACE, SPACE, SPACE},
- {SPACE, SPACE, SPACE},
- {SPACE, SPACE, SPACE}
- };
- int main(void) {
- do {
- //Display matrix
- disp_matrix();
- //Player
- player();
- //Check
- done = check();
- if(done != SPACE)
- break;
- pc();
- done = check();
- }
- while(done == SPACE);
- if(done == 'X') {
- printf("\nYou win!\n");
- disp_matrix();
- return 0;
- }
- else {
- printf("\nYou lose!\n");
- disp_matrix();
- printf("\n\nArray: %s", &matrix);
- return 0;
- }
- }
- //Player
- void player(void) {
- int x, y;
- printf("\nEnter you coords (X)\n");
- scanf("%d", &x);
- printf("Enter you coords (Y)\n");
- scanf("%d", &y);
- printf("\n");
- x--;
- y--;
- if(x < 0 || y < 0 || x > 2 || y > 2 || matrix[x][y] != SPACE || matrix[x][y] == 'O') {
- printf("Error move!");
- player();
- }
- else {
- matrix[x][y] = 'X';
- --ininum;
- }
- }
- //Computer
- int pc(void) {
- static int ix, iy;
- iy = 0;
- for(static int ix = 0; ix < 3; ix++) {
- if(iy < 3 || ininum > 0){
- if(matrix[ix][iy] == SPACE) {
- matrix[ix][iy] = 'O';
- --ininum;
- iy++;
- return 0;
- }
- else {
- iy = 0;
- continue;
- }
- }
- }
- }
- //Matrix display
- void disp_matrix(void) {
- for(register int x = 0; x < 3; x++) {
- printf("\n%c | %c | %c\n", matrix[x][0], matrix[x][1], matrix[x][2]);
- if(x < 2)
- printf("\n- | - | -\n");
- }
- }
- //Check party
- int check(void) {
- register int t;
- for(t = 0; t < 3; t++) {
- char *p;
- p = &matrix[0][t];
- if(*p == *(p + 1) && *(p + 1) == *(p + 2))
- return *p;
- }
- for(t = 0; t < 3; t++) {
- char *p;
- p = &matrix [t][0];
- if(*p == *(p+3) && *(p+3) == *(p+6))
- return *p;
- }
- if(matrix[0][0] == matrix[1][1] && matrix[1][1] == matrix[2][2])
- return(matrix[0][0]);
- if(matrix[0][2] == matrix[1][1] && matrix[1][1] == matrix[2][0])
- return(matrix[0][2]);
- return SPACE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement