/* Mikhail Golitsine 02/13/98 The Game of Matchsticks. Computer learns to play. Note: Copyright 1998 to Mikhail V. Golitsine The basic idea was taken from the book by W.S.Brainerd "Introduction to Computer Programming" Copyright 1979. Anyone is welcome to copy and as long as reference to me is left in the file. If the code is changed or edited then it must be stated in the file that it has been changed from my original. */ #include void Playgame(); void Human_move(); void Computer_move(); void Modify_Experience(); int losses; int wins; // number of times the player had won int total_moves; // number of times the computer had won int num_of_matches; int outcome; const int starting_num_of_matches = 20; int matches_left[starting_num_of_matches]; int success[starting_num_of_matches]; void main(){ char another_game = 'y'; losses = 0; wins = 0; for (int i = 0; i> another_game; cout << endl; } cout << "Match Statistics:" << endl; cout << "You won " << wins << " games." << endl; cout << "Computer won " << losses << " games." << endl; for (i = 0; i> remove; if ((remove <= 3) && (remove >=1) && (remove <= num_of_matches)) { // errorchecking num_of_matches = num_of_matches - remove; cout << "That leaves " << num_of_matches << " matches." << endl; legal_move = 1; break; } else{ cout << "You can't take " << remove << " matches." << endl; cout << "Move again" << endl; cout << "There are " << num_of_matches << " matches left." << endl; } } // end of while move that was checking for a legal move } // end of Human_move void Computer_move(){ int bestmove = 1; for (int move = 2; move <=3; move++){ if (move > num_of_matches) break; if (success[num_of_matches-move] > success[num_of_matches-bestmove]) bestmove = move; } num_of_matches = num_of_matches - bestmove; cout << "I take " << bestmove << " matches, leaving "<< num_of_matches << "." << endl; } void Modify_Experience(){ if (outcome == 1) for (int i = 1; i < total_moves; i=i+2) success[matches_left[i]]++; if (outcome == 0) for (int i = 1; i < total_moves-1; i=i+2) success[matches_left[i]]--; }