/* Program to solve the matchstick game for up to 7 matches. */ #define MAXSIZE (7) /* Max. matches on each side of the space */ #include #include /* for exit() */ typedef struct { signed char piece[MAXSIZE*2+1]; /* MAXSIZE each way + space */ /* Note: a 'piece' is either a matchstick or the vacant space. */ char spaceloc; /* where is the space? */ } gamestate; gamestate current, win_state; /* The game during play & target position */ int gamesize; /* matches each way */ int positions; /* Size of playing area (gamesize*2+1) */ void initialise(void); /* Sets up current & win_state */ int seek_win(void); /* Returns 1 for win, 0 for failure */ main() { printf(" The Matchstick Game\nHow many matches (max %d)? ", MAXSIZE); do { scanf("%d", &gamesize); if (gamesize>MAXSIZE) { printf("Too big (maximum %d), please re-enter: ", MAXSIZE); } } while (gamesize>MAXSIZE); positions = gamesize*2 + 1; initialise(); /* Set up current and win_state to represent the first and final state of the matches. */ seek_win(); /* Do it all! */ return 0; } /****** initialise: ********************** On entry: gamesize is the number of matches each way, positions is gamesize*2+1. On exit: Variables current & win_state are set up to represent the initial & final states respectively for a game of size gamesize. */ void initialise(void) { /* current should be as shown below. (e.g. for gamesize == 3): +----+----+----+----+----+----+----+ | 1 | 1 | 1 | 0 | -1 | -1 | -1 | +----+----+----+----+----+----+----+ The win_state has the match directions reversed. */ int i, rhs; rhs = gamesize + 1; for (i=0; i= 0 && /* Space not fallen off LHS */ spaceloc < positions && /* Space not fallen off RHS */ spaceloc != current_spaceloc && /* Space HAS been moved */ current.piece[spaceloc] * (current_spaceloc-spaceloc) > 0 /* Matchstick is moving forward. (Think about it.) */ ); } /****** alter_position: ***************** On entry: spaceloc is location of match to be moved to current_spaceloc. On exit: current structure updated to reflect the move. */ void alter_position(char spaceloc, char current_spaceloc) { current.piece[current_spaceloc] = current.piece[spaceloc]; /* Shift piece into space */ current.piece[spaceloc] = 0; /* Set its old spot empty */ current.spaceloc = spaceloc; /* Record new position of space */ } /****** undo_changes: ****************** On entry: spaceloc is the old location of the match that was moved into the space at current_spaceloc. On exit: current structure updated to undo the move. */ void undo_changes(char spaceloc, char current_spaceloc) { current.spaceloc = current_spaceloc; /* Reset space position */ current.piece[spaceloc] = current.piece[current_spaceloc]; /* Reverse piece */ current.piece[current_spaceloc] = 0; /* Put space where it was before */ } /****** found_a_win: ****************** On entry: current is the current state of the game, win_state is the target state (the position when a win is found). On Exit: Returns 1 if current is the winning position, 0 otherwise. */ int found_a_win(void) { int pos; if (current.spaceloc != win_state.spaceloc) { return 0; /* If the spaces are at different places, states differ */ } for (pos=0; pos" */ int i; for (i=0; i "); break; case 0: printf(" "); break; case 1: printf("<--- "); } } putchar('\n'); /* End the line */ }