// Author: Hannes Kirsman // Year: 2004 #include #include #include #include #include using namespace std; enum{bOff,bGreen,bRed,bEmpty,bGreenKing,bRedKing}; class cCheckers { // these should be in the functions. int i, k, e, f, g; int cCheckers::is_button (int m[0][8], int x, int y); int cCheckers::is_empty (int m[0][8], int x, int y); int cCheckers::is_in_array (int m[0][8], int from_x, int from_y, int to_x, int to_y); char cCheckers::int_to_char (int integer); void cCheckers::colorize(char symbol); // changes for example a,A to 0; int cCheckers::input_to_standard(int character); public: // don't know if this works. haven't ended any game yet :) int cCheckers::is_game_over(int m[][8]); void cCheckers::change_output (int m[][8], char n[][38]); char cCheckers::write (char[][38]); int cCheckers::make_move (int m[][8], int from_x, int from_y, int to_x, int to_y, int whos_turn); int cCheckers::must_make_move (int m[][8], int which_button); int cCheckers::change_side (int state); char cCheckers::draw_meny (void); void cCheckers::write_error_message (int error_id); void cCheckers::write_game_status (int m[8][8]); void cCheckers::find_button (int m[0][8], int button, int button_location[2]); void cCheckers::make_auto_move (int m[0][8], int from_x, int from_y, int whos_turn); }; int main() { int i, k; int checkboard[8][8] = {{0,3,0,3,0,3,0,3}, {3,0,3,0,3,0,3,0}, {0,3,0,3,0,3,0,3}, {3,0,3,0,3,0,3,0}, {0,3,0,3,0,3,0,3}, {3,0,3,0,3,0,3,0}, {0,3,0,3,0,3,0,3}, {3,0,3,0,3,0,3,0} }; // FAILIST LUGEMINE char buffer[256]; ifstream examplefile ("algseis.txt"); if (! examplefile.is_open()) { cout << "Error opening file"; exit (1); } for (i=0; i<8; i++ ) { examplefile.getline (buffer,9); for (k=0; k<8; k++ ) { if ( buffer[k] == 86) checkboard[i][k] = 2; if ( buffer[k] == 77) { checkboard[i][k] = 4; } } } char input_at_menu[1]; char input[6]; int whos_turn = 0; // 0 for green, 1 for red int button_location[2]; system("cls"); // works only in windows cCheckers myBoard; // menyy while ( input_at_menu[0] != (char) 49 && input_at_menu[0] != (char) 50 ) { myBoard.draw_meny(); cin.getline(input_at_menu, 2); } system("cls"); // lets exit the program if ( input_at_menu[0] == (char) 50 ) return 0; char checkboard_output[20][38]; int c; int button = 4; while (getch() ) { system("cls"); myBoard.find_button (checkboard, button, button_location); myBoard.make_auto_move (checkboard, button_location[0], button_location[1], whos_turn); whos_turn = myBoard.change_side(whos_turn); if (whos_turn == 0 ) button = 4; if (whos_turn == 1 ) button = 2; myBoard.change_output(checkboard, checkboard_output); myBoard.write (checkboard_output); } getch(); return 0; } void cCheckers::write_game_status (int m[8][8]) { int i, k; int count_red_buttons = 0, count_green_buttons = 0; for (i=0;i<8;i++) { for (k=0;k<8;k++) { if ( m[i][k] == 1 || m[i][k] == 4 ) count_red_buttons++; if ( m[i][k] == 2 || m[i][k] == 5 ) count_green_buttons++; } } cout << "Seis: " << 12 - count_green_buttons << " - " << 12 - count_red_buttons << "\n"; } char cCheckers::draw_meny (void) { system("cls"); textcolor(1); cout << "\n\n\n\n"; cout << " _/ _/ _/" << endl; cout << " _/ _/ _/_/_/ _/_/_/ _/_/" << endl; cout << " _/_/ _/ _/ _/ _/ _/_/_/_/" << endl; cout << " _/ _/ _/ _/ _/ _/ _/" << endl; cout << " _/ _/ _/_/_/ _/_/_/ _/_/_/" << endl; cout << " alpha" << endl; cout << " Hannes Kirsman" << endl; cout << "\n\n\n\n"; textcolor(7); cout << " 1. M2ngi" << endl; cout << " 2. L8peta" << endl; } int cCheckers::is_empty (int m[0][8], int x, int y) { if (m[x][y] == bEmpty) return true; else return false; } int cCheckers::is_game_over(int m[][8]) { for (i=0; i<9; i++) { for (k=0; k<9; k++) { if (m[i][k] == 1 || m[i][k] == 2) return true; k=9; i=9; } } } int cCheckers::is_button (int m[0][8], int x, int y) { if (m[x][y] == bGreen || m[x][y] == bRed || m[x][y] == bGreenKing || m[x][y] == bRedKing ) return true; else return false; } char cCheckers::int_to_char (int integer) { char character; if ( integer == bOff ) { character = (char)176; } if ( integer == bGreen ) { character = 'M'; } if ( integer == bRed ) { character = 'V'; } if ( integer == bEmpty ) { character = ' '; } if ( integer == bGreenKing ) { character = 'M'; } if ( integer == bRedKing ) { character = 'T'; } return character; } void cCheckers::change_output(int m[][8], char n[][38]) { // believe me, I know it sucks :) // this will add " a b c d e f g h" to the array n[0][0]-n[0][34] // " a" n[0][0] = ' '; n[0][1] = ' '; n[0][2] = ' '; n[0][3] = ' '; n[0][4] = ' '; n[0][5] = 'a'; // " b" n[0][6] = ' '; n[0][7] = ' '; n[0][8] = ' '; n[0][9] = 'b'; // " c" n[0][10] = ' '; n[0][11] = ' '; n[0][12] = ' '; n[0][13] = 'c'; // " d" n[0][14] = ' '; n[0][15] = ' '; n[0][16] = ' '; n[0][17] = 'd'; // " e" n[0][18] = ' '; n[0][19] = ' '; n[0][20] = ' '; n[0][21] = 'e'; // " f" n[0][22] = ' '; n[0][23] = ' '; n[0][24] = ' '; n[0][25] = 'f'; // " g" n[0][26] = ' '; n[0][27] = ' '; n[0][28] = ' '; n[0][29] = 'g'; // " h" n[0][30] = ' '; n[0][31] = ' '; n[0][32] = ' '; n[0][33] = 'h'; n[0][34] = ' '; n[0][35] = ' '; n[0][36] = ' '; n[0][37] = ' '; // this will add " ---------------------------------" to the array n[1][0]-n[1][35] // " " n[1][0] = ' '; n[1][1] = ' '; n[1][2] = ' '; // "---------------------------------" for (i=3; i<35; i++) { n[1][i] = '-'; } n[1][35] = ' '; n[1][36] = ' '; n[1][36] = ' '; n[1][36] = ' '; n[1][37] = ' '; // now we'll start to process the checkboard array that contains the actual status of the game f = 2; for (i=0; i<8; i++) { e = 3; for (k=0; k<8; k++) { n[f][0] = (char)(i+49); n[f][1] = ' '; n[f][2] = '|'; n[f][e++] = int_to_char(m[i][k]); n[f][e++] = int_to_char(m[i][k]); n[f][e++] = int_to_char(m[i][k]); n[f][e++] = int_to_char(m[i][k]); n[f][35] = '|'; n[f][36] = ' '; n[f][37] = (char)(i+49); n[f+1][0] = ' '; for (g=1; g<37; g++) { n[f+1][g] = n[f][g]; } n[f+1][37] = ' '; } f= f + 2; } // this will add " ---------------------------------" to the array n[1][0]-n[1][35] // " " n[18][0] = ' '; n[18][1] = ' '; n[18][2] = ' '; // "---------------------------------" for (i=3; i<35; i++) { n[18][i] = '-'; } n[18][35] = ' '; n[18][36] = ' '; n[18][36] = ' '; n[18][36] = ' '; n[18][37] = ' '; // believe me, I know it sucks :) // this will add " a b c d e f g h" to the array n[0][0]-n[0][37] // " a" n[19][0] = ' '; n[19][1] = ' '; n[19][2] = ' '; n[19][3] = ' '; n[19][4] = ' '; n[19][5] = 'a'; // " b" n[19][6] = ' '; n[19][7] = ' '; n[19][8] = ' '; n[19][9] = 'b'; // " c" n[19][10] = ' '; n[19][11] = ' '; n[19][12] = ' '; n[19][13] = 'c'; // " d" n[19][14] = ' '; n[19][15] = ' '; n[19][16] = ' '; n[19][17] = 'd'; // " e" n[19][18] = ' '; n[19][19] = ' '; n[19][20] = ' '; n[19][21] = 'e'; // " f" n[19][22] = ' '; n[19][23] = ' '; n[19][24] = ' '; n[19][25] = 'f'; // " g" n[19][26] = ' '; n[19][27] = ' '; n[19][28] = ' '; n[19][29] = 'g'; // " h" n[19][30] = ' '; n[19][31] = ' '; n[19][32] = ' '; n[19][33] = 'h'; n[19][34] = ' '; n[19][35] = ' '; n[19][36] = ' '; n[19][37] = ' '; } // this is for coloring the checkboard void cCheckers::colorize(char symbol) { if (symbol=='M') { textcolor(10); // green textbackground(0); } if (symbol=='V') { textcolor(12); // red textbackground(0); } if (symbol==(char)176) { textcolor(0); textbackground(15); } if (symbol==' ') { textcolor(0); textbackground(0); } if (symbol=='t') { textcolor(10); textbackground(0); } if (symbol=='T') { textcolor(12); textbackground(0); } } char cCheckers::write (char n[0][38]) { int i, k; for (i=0; i<20; i++) { for (k=0; k<38; k++) { colorize (n[i][k]); cout << n[i][k]; textcolor(7); textbackground(0); } cout << endl; } } void cCheckers::write_error_message (int error_id) { if ( error_id == 2 ) cout << "Kabelaud ei ole nii suur!"; if ( error_id == 3 ) cout << "Sa ei valinud nuppu!"; if ( error_id == 4 ) cout << "Yritame ikka tyhja koha peale k2ia!"; if ( error_id == 5 ) cout << "Hetkel on rohelise kord k2ia!"; if ( error_id == 6) cout << "Hetkel on punase kord k2ia!"; if ( error_id == 7) cout << "Horisontaalset ei ole siin m2ngus v8imalik liikuda!"; if ( error_id == 8) cout << "Vertikaalselt ei ole siin m2ngus v8imalik liikuda!"; if ( error_id == 9) cout << "Tagasi ei ole v8imalik k2ia!"; if ( error_id == 10) cout << "Yks samm korraga palun!"; if ( error_id == 11) cout << "Kmkh, me m2ngime kabet!"; cout << " Proovi uuesti.\n"; } // thanks to this function for example coordinates 32 41 = 3b 4a int cCheckers::input_to_standard(int character) { // 97 in ascii is a, 98 b and so forth if (character == 97) return 0; if (character == 98) return 1; if (character == 99) return 2; if (character == 100) return 3; if (character == 101) return 4; if (character == 102) return 5; if (character == 103) return 6; if (character == 104) return 7; if (character == 65) return 0; if (character == 66) return 1; if (character == 67) return 2; if (character == 68) return 3; if (character == 69) return 4; if (character == 70) return 5; if (character == 71) return 6; if (character == 72) return 7; if (character == 49) return 0; if (character == 50) return 1; if (character == 51) return 2; if (character == 52) return 3; if (character == 53) return 4; if (character == 54) return 5; if (character == 55) return 6; if (character == 56) return 7; } // changes 1 to 0 and vice versa int cCheckers::change_side (int state) { if (state==0) state = 1; else state = 0; return state; } // not complete yet int cCheckers::must_make_move (int m[0][8], int which_button) { for (i=0; i<8; i++) { for (k=0; k<8; k++) { } } return 0; } int cCheckers::is_in_array (int m[0][8], int from_x, int from_y, int to_x, int to_y) { // we can't work with dimensions bigger than our array if ( from_x > 7 || from_y > 7 || to_x > 7 || to_y > 7 || from_x < 0 || from_y < 0 || to_x < 0 || to_y < 0 ) return false; else return true; } // This moves the buttons. The most important function after main(). int cCheckers::make_move (int m[][8], int from_x, int from_y, int to_x, int to_y, int whos_turn) { int temp; from_y = input_to_standard(from_y); to_y = input_to_standard(to_y); // RULES; otherwise you could do anything you want if ( is_in_array (m, from_x, from_y, to_x, to_y) == false ) return 2; if ( is_button(m, from_x, from_y) == false ) return 3; if ( is_empty(m, to_x, to_y) == false ) return 4; if ( whos_turn == 0 && (m[from_x][from_y] != 1 && m[from_x][from_y] != 4) ) return 5; if ( whos_turn == 1 && (m[from_x][from_y] != 2 && m[from_x][from_y] != 5) ) return 6; // buttons can't move horisontally, nor vertically if ( from_y == to_y) return 7; if ( from_x == to_x ) return 8; // will stop the green button from making false moves if ( m[from_x][from_y] == bGreen ) { if ( from_x > to_x ) return 9; // next if will stop the button from making diagonal moves over 1 step if ( to_x - from_x > 1 && m[from_x+1][from_y+1] != bRed && m[from_x+1][from_y-1] != bRed) return 10; } // will stop the red button from making false moves if ( m[from_x][from_y] == bRed ) { if ( from_x < to_x ) return 9; // next if will stop the button from making diagonal moves over 1 step if ( from_x - to_x > 1 && m[from_x-1][from_y+1] != bGreen && m[from_x-1][from_y-1] != bGreen) return 10; } /* I think it's not important if ( m[from_x][from_y] == bRed ) { if ( from_x < to_x ) return false; } */ // some final rules if ( to_y - from_y > 2 ) return 11; if ( from_y - to_y > 2 ) return 11; // green will become king if ( m[from_x][from_y]== 1 && to_x == 7) m[from_x][from_y] = 4; // red will become king if ( m[from_x][from_y]== 2 && to_x == 0) m[from_x][from_y] = 5; // killing other button. :) if ( from_x - to_x == 2 && from_y - to_y == 2) m[from_x - 1][from_y-1] = 3; if ( from_x - to_x == 2 && from_y + 2 == to_y) m[from_x - 1][from_y+1] = 3; if ( from_x + 2 == to_x && from_y - 2 == to_y) m[from_x + 1][from_y-1] = 3; if ( from_x + 2 == to_x && from_y + 2 == to_y) m[from_x + 1][from_y+1] = 3; // and finally, lets make the move temp = m[to_x][to_y]; m[to_x][to_y] = m[from_x][from_y]; m[from_x][from_y] = temp; return true; } void cCheckers::find_button (int m[0][8], int button, int button_location[2]) { int i, k, l = 0; int random; srand(time(0)); random = rand()%4; for (i=0; i<8; i++) { for (k=0; k<8; k++ ) { if ( m[i][k] == button ) { button_location[0] = i; button_location[1] = k; if ( button = 4 ) { button_location[0] = i; button_location[1] = k; } if ( button = 2 ) { if ( l == random ) { button_location[0] = i; button_location[1] = k; i = 9; } l++; } } } } } void cCheckers::make_auto_move (int m[0][8], int from_x, int from_y, int whos_turn) { int loop_check = 0; int to_x = from_x, to_y = from_y; while ( loop_check < 4 ) { if ( loop_check == 0) { to_x = from_x + 1; to_y = from_y + 1; } if ( loop_check == 1) { to_y = from_y - 1; } if ( loop_check == 2) { to_x = from_x - 1; } if ( loop_check == 3) { to_y = from_y + 1; } if ( make_move (m, from_x, from_y, to_x, to_y, whos_turn) == 1 ) { } loop_check++; } }