+/EE306
intro to computing
git clone https://git.julia.blue/+/EE306
30b1299
master
1#include <stdio.h> 2#include <stdlib.h> 3 4int turn = -1 ;/* -1 = player 1, 1 = player 2*/ 5int board [6 ][6 ]; 6 7void display (){ 8for (int i = 0 ;i < 6 ;i ++ ) { 9for (int j = 0 ;j < 6 ;j ++ ) { 10if (board [i ][j ]< 0 ) { 11putc ('O' ,stdout ); 12 }else if (board [i ][j ]> 0 ) { 13putc ('X' ,stdout ); 14 }else { 15putc ('-' ,stdout ); 16 } 17if (j < 5 ) { 18putc (' ' ,stdout ); 19 } 20 } 21putc ('\n' ,stdout ); 22 } 23} 24 25int can_move (int col ){ 26return board [0 ][col ]== 0 ; 27} 28 29void execute_move (int col ){ 30int n = 5 ; 31while (n >=0 ) { 32if (board [n ][col ]== 0 ) { 33board [n ][col ]= turn ; 34return ; 35 } 36n -- ; 37 } 38} 39 40int check_win (){ 41for (int i = 0 ;i < 3 ;i ++ ) { 42for (int j = 0 ;j < 3 ;j ++ ) { 43int diagup = 0 ; 44int diagdown = 0 ; 45for (int k = 0 ;k < 4 ;k ++ ) { 46diagup += board [5 - i - k ][j + k ]; 47diagdown += board [i + k ][j + k ]; 48 } 49if (abs (diagup )== 4 || abs (diagdown )== 4 ) { 50return 1 ; 51 } 52 } 53 } 54for (int i = 0 ;i < 3 ;i ++ ) { 55for (int j = 0 ;j < 6 ;j ++ ) { 56int vert = 0 ; 57int hor = 0 ; 58for (int k = 0 ;k < 4 ;k ++ ) { 59vert += board [j ][i + k ]; 60hor += board [i + k ][j ]; 61 } 62if (abs (vert )== 4 || abs (hor )== 4 ) { 63return 1 ; 64 } 65 } 66 } 67return 0 ; 68} 69 70int check_full (){ 71for (int i = 0 ;i < 6 ;i ++ ) { 72if (can_move (i )) { 73return 0 ; 74 } 75 } 76return 1 ; 77} 78 79int main (){ 80char input ; 81while (1 ) { 82display (); 83while (1 ) { 84printf ("Player %d, choose a column: " , (turn + 3 ) /2 ); 85input = getc (stdin ); 86getc (stdin ); 87if (input > 48 && input < 55 ) { 88input -= 49 ; 89if (can_move (input )) { 90execute_move (input ); 91break ; 92 } 93 } 94printf ("Invalid move. Try again.\n" ); 95 } 96if (check_win ()) { 97display (); 98printf ("Player %d Wins.\n" , (turn + 3 ) /2 ); 99break ; 100 }else if (check_full ()) { 101display (); 102printf ("Tie Game\n" ); 103break ; 104 } 105turn = - turn ; 106 } 107return 0 ; 108}