C language course design homework source code (self-storage)

C language course design homework source code (self-storage)

# include <algorithm> # include <iostream> # include <math.h> # include <windows.h> # include <stdio.h> using namespace std; char mp[ 15 ][ 15 ]; const int Len = 15 ; const int win_count = 5 ; bool Judge ( const char map[ 15 ][ 15 ], char a) //Judge whether the game is over { //printf("## %c\n", a); for ( int i = 0 ; i <Len; i++) { for ( int j = 0 ; j <Len; j++) { if (map[i][j] == a) { int count_a = 1 ; //1. ----------------------------------------- ----------------------- //Vertical direction for ( int x = i- 1 , times = 0 ; x >= 0 ; x--, times++) { if (map[x][j] == a) { count_a++; } else break ; if (times> win_count- 1 ) break ; } for ( int y = i + 1 , times = 0 ; y <Len; y++, times++) { if (map[y][j] == a) { count_a++; } else break ; if (times> win_count- 1 ) break ; } if (count_a >= win_count) { return true ; } //2. ---------------------------------------------- ---------------------- //Horizontal direction count_a = 1 ; for ( int x = j- 1 , times = 0 ; x >= 0 ; x- -, times++) { if (map[i][x] == a) { count_a++; } else break ; if (times> win_count- 1 ) break ; } for ( int y = j + 1 , times = 0 ; y <Len; y++, times++) { if (map[i][y] == a) { count_a++; } else break ; if (times> win_count- 1 ) break ; } if (count_a >= win_count) { return true ; } //3. ---------------------------------------------- --------------------- // Upward and downward direction count_a = 1 ; for ( int x = i- 1 , y = j- 1 , times = 0 ; i >= 0 && j >= 0 ; x--, y--, times++) { if (map[x][y] == a) { count_a++; } else break ; if (times> win_count- 1 ) break ; } for ( int x = i + 1 , y = j + 1 , times = 0 ; i <Len && j <Len; x++, y++, times++) { if (map[x][y] == a) { count_a++; } else break ; if (times> win_count- 1 ) break ; } if (count_a >= win_count) { return true ; } //4. ---------------------------------------------- -------------------- // Downward and upward direction count_a = 1 ; for ( int x = i + 1 , y = j- 1 , times = 0 ; x <Len && y >= 0 ; x++, y--, times++) { if (map[x][y] == a) { count_a++; } else break ; if (times> win_count- 1 ) break ; } for ( int x = i- 1 , y = j + 1 , times = 0 ; x >= 0 && y <Len; x--, y++, times++) { if (map[x][y] == a) { count_a++; } else break ; if (times> win_count- 1 ) break ; } if (count_a >= win_count) { return true ; } //------------------------------------------------ ------------------- } } } return false ; } void f () //Print the rules of the game { printf ( "------------------------------------- -------------------\n" ); printf ( "| _________________________________ |\n" ); printf ( " | | The Game will begin!!! | |/n" ); printf ( "| |_______________________________| |\n" ); printf ( " | *game rules* |\n" ); printf ( "| Please take turns to enter the coordinates of the chess pieces (separated by spaces) |\n" ); printf ( "| black (@) chess:@ |\n" ); printf ( " | white (O) chess: O |\n" ); printf ( "| black chess first |\n" ); printf ( "------------------------------------------------- --------\n" ); } int main () { f (); for ( int i = 10 ; i >= 1 ;i--) Sleep ( 1000 ), printf ( "There are %2d seconds to start the game\n" , i); system ( " cls" ); for ( int i = 0 ; i <= 15 ; i++) for ( int j = 0 ; j <= 15 ; j++) mp[i][j] = '' ; //Print the result (debugging) printf ( "" ); for ( int i = 1 ; i <= 15 ; i++) printf ( "%-4d" , i); printf ( "\n" ); printf ( "____________________________________________________________\n" ); for ( int i = 0 ; i < 15 ; i++) { if (i >= 0 ) printf ( "%-3d" , i + 1 ); else printf ( "" ); printf ( "|" ); for ( int j = 0 ; j < 15 ; j++) printf ( "%-2c|" , mp[i][j]); printf ( "\n ____________________________________________________________\n" ); } printf ( "\n\n" ); int heng, zong, flag = 0 ; printf ( "Please enter the black (@) chess coordinates:\n" ); mp[ 15 ][ 15 ] = '' ; while ( scanf ( "%d%d" , &zong, &heng)) { //Enter //printf("%" //system("cls");) while (heng < 1 || zong < 1 || heng> Len || zong> Len || mp[zong -1 ][heng -1 ] != '' ) { //printf("h=%dz=%dl=%d %c\n", heng, zong, Len, mp[zong][heng]); printf ( "Input error,input again!!!\n" ); if (flag% 2 == 0 ) printf ( "Please enter black (@) chess coordinates:\n" ); else printf ( "Please enter white (O) chess coordinates:\n" ); scanf ( "% d%d" , &zong, &heng); } cout << "Input success!!!\n" ; system ( "cls" ); if (flag% 2 == 0 ) mp[zong- 1 ][heng- 1 ] = '@' ; else mp[zong- 1 ][heng- 1 ] = 'O' ; //Print a new map printf ( "" ); for ( int i = 1 ; i <= 15 ; i++) printf ( "%-4d" , i); printf ( "\n" ); printf ( "____________________________________________________________\n" ); for( int i = 0 ; i < 15 ; i++) { if (i >= 0 ) printf ( "%-3d" , i + 1 ); else printf ( "" ); printf ( "|" ); for ( int j = 0 ; j < 15 ; j++) printf ( "%-2c|" , mp[i][j]); printf ( "\n ____________________________________________________________\n" ); } printf ( "\n\n" ); //Make a judgment to see which side wins if (flag% 2 == 0 ) { if ( Judge (mp, '@' )) { printf ( "Black wins!!!/n" ); break ; } } else { if ( Judge (mp, 'O' )) { printf ( "White wins!!!/n" ); break ; } } flag++; if (flag% 2 == 0 ) printf ( "Please enter black (@) chess coordinates:\n" ); else printf ( "Please enter white (O) chess coordinates:\n" ); } printf ( "Game over!!!\n" ); char p[ 100 ]; gets (p); return 0 ; } Copy code