#include #define MAXLINE 1000 /* maximum linput line size */ #define HORIZONTAL 13 #define VERTICAL 21 int getline(char line[], int maxline); void copy(char to[HORIZONTAL][VERTICAL], char from[HORIZONTAL][VERTICAL], int h, int v); void copyd(char to[HORIZONTAL][VERTICAL][3][3][2], char from[HORIZONTAL][VERTICAL], int h, int v, int h1, int v1, int r); void piece(char board[HORIZONTAL][VERTICAL], char line[MAXLINE], char current); void play(char board[HORIZONTAL][VERTICAL], char line[MAXLINE], char current, int valid[], int ib, int jb, int start, int hor, int ver); void draw(char board[HORIZONTAL][VERTICAL], int h, int v); int len; int max; int i; int j; int ib; int jb; int ic; int jc; int id; int jd; int start; int gen; char line[MAXLINE]; char longest[MAXLINE]; char board[HORIZONTAL][VERTICAL]; char current[HORIZONTAL][VERTICAL]; char dest[HORIZONTAL][VERTICAL]; char wm; char level; int valid[1]; main() { level == 'm'; gen == '5'; printf("%s","SOCCER MILLENIUM (X to move)\n"); wm = 'X'; for (i=0; i<=HORIZONTAL; i=i+1) { for (j=0; j<=VERTICAL; j=j+1) { board[i][j]=' '; } } for (i=2; i<=HORIZONTAL-1; i=i+1) { board[i][0]='A'+i-2; } for (j=1; j<=VERTICAL-2; j=j+1) { board[0][j]='a'+j-1; } board[7][2]='X'; board[6][4]='X'; board[8][4]='X'; board[4][5]='X'; board[10][5]='X'; board[7][6]='X'; board[5][7]='X'; board[9][7]='X'; board[3][8]='X'; board[7][8]='X'; board[11][8]='X'; board[7][10]='*'; board[3][12]='O'; board[7][12]='O'; board[11][12]='O'; board[5][13]='O'; board[9][13]='O'; board[7][14]='O'; board[4][15]='O'; board[10][15]='O'; board[6][16]='O'; board[8][16]='O'; board[7][18]='O'; ib=5; jb=9; start=0; draw(board,HORIZONTAL,VERTICAL); copy(current,board,HORIZONTAL,VERTICAL); printf("Command (h for help): "); while ((len=getline(line,MAXLINE))>=0 && ((line[0]!='q') || (len != 1))){ if (len == 1) { if (line[0]=='h') { printf("%s","h help\n"); printf("%s","i instructions\n"); printf("%s","r current position\n"); printf("%s","n new game\n"); printf("%s","b beginner adversary\n"); printf("%s","m intermediate adversary\n"); printf("%s","a advanced adversary\n"); printf("%s","0 adversary not random\n"); printf("%s","1-9 adversary random every so many moves\n"); printf("%s","x adversary executes move\n"); printf("%s","q quit\n"); printf("%s","Aa move to (move player in Aa)\n"); printf("%s","Aa* ball to (move ball with player in Aa)\n"); printf("%s","AaBb move to make (move player in Aa to Bb)\n"); printf("%s","Aa*Bb ball to make (move ball with player in Aa to Bb)\n"); } if (line[0]=='i') { printf("%s","MILLENIUM SOCCER (by Tomas Feder)\n"); printf("%s","\n"); printf("%s","MILLENIUM SOCCER is a two-player strategy game. It is played on a board of\n"); printf("%s","17 rows by 11 columns, with two extra rows at the two ends of the board that\n"); printf("%s","are not considered part of the board and represent the goals of the\n"); printf("%s","two teams.\n"); printf("%s","\n"); printf("%s","The set of pieces consists of two groups of 11 pieces each, representing two\n"); printf("%s","teams, and an additional piece, representing the ball.\n"); printf("%s","\n"); printf("%s","The two players play in alternation, and in each move they move one of\n"); printf("%s","the eleven pieces representing their team, and perhaps also the ball. In\n"); printf("%s","order to move only one of the eleven pieces in the team, the player selects\n"); printf("%s","one of them and moves it along a straight line, without moving over any\n"); printf("%s","other piece, including the ball, or the boundary of the board. This\n"); printf("%s","movement is identical to the movement of the queen in the game of chess.\n"); printf("%s","\n"); printf("%s","In order to move the ball, the player selects one of the eleven pieces in\n"); printf("%s","the team for which it is possible to move along a straight line, without\n"); printf("%s","moving over any other piece, and reach the square occupied by the ball. The\n"); printf("%s","player now has a choice between a *short shot* or a *long shot*: If the\n"); printf("%s","chosen piece moved some number k of squares (for example k=6), then the ball\n"); printf("%s","may be moved k squares as well (k=6 in the example), or 2k squares instead\n"); printf("%s","(2k=12 in the example). The ball is moved in a straight line in ay direction\n"); printf("%s","(not depending on the direction followed by the piece that reached the\n"); printf("%s","ball), but without moving over another piece or over the boundary of the\n"); printf("%s","board; if one of these two excluded situations would arise if the wall were\n"); printf("%s","to continue in its current direction one more square, the the ball must\n"); printf("%s","continue along a new chosen direction, and so on until the chosen number k\n"); printf("%s","or 2k of squares that the ball must move has been completed.\n"); printf("%s","\n"); printf("%s","The rules with regards to the boundaries of the board next to the two goals\n"); printf("%s","are the same as the rules for the other two boundaries, with the exception\n"); printf("%s","that if the ball completes the exact chosen number (k or 2k) by continuing\n"); printf("%s","on its current straight line direction and entering one of the goals, then\n"); printf("%s","it can do so, scoring a goal (and leaving the boundaries of the board).\n"); printf("%s","\n"); printf("%s","To start the game, one of the two playes begins, this player must move\n"); printf("%s","the ball (with the only piece that can reach the ball), and by means of a\n"); printf("%s","*short shot*.\n"); printf("%s","\n"); } if (line[0]=='r') { printf("%s","current position\n"); draw(current,HORIZONTAL,VERTICAL); } if (line[0]=='n') { printf("%s","new game (X to move)\n"); wm = 'X'; copy(current,board,HORIZONTAL,VERTICAL); draw(current,HORIZONTAL,VERTICAL); start = 0; ib = 5; jb = 9; } if (line[0]=='b') { level = 'b'; } if (line[0]=='m') { level = 'm'; } if (line[0]=='a') { level = 'a'; } if ((line[0] >= '0') && (line[0] <= '9')) { gen = line[0] - '0'; } if (line[0]=='x') { } } if (len == 2) { putchar(line[0]); putchar(line[1]); printf("%s"," move to\n"); if (start == 1) { copy(dest,current,HORIZONTAL,VERTICAL); piece(dest, line, wm); ic = line[0]-'A'; jc = line[1]-'a'; if ((ic >= 0) && (ic <= 10) && (jc >= 0) && (jc <= 18) && (current[ic+2][jc+1]==wm)) { draw(dest,HORIZONTAL,VERTICAL); }; }; } if (len == 3) { putchar(line[0]); putchar(line[1]); putchar(line[2]); printf("%s"," ball to\n"); copy(dest, current, HORIZONTAL, VERTICAL); play(dest, line, wm, valid, ib, jb, start, HORIZONTAL, VERTICAL); if (valid[0] == 1) { draw(dest,HORIZONTAL,VERTICAL); }; } if (len == 4) { putchar(line[0]); putchar(line[1]); putchar(line[2]); putchar(line[3]); printf("%s"," move to make\n"); if (start == 1) { copy(dest,current,HORIZONTAL,VERTICAL); piece(dest, line, wm); ic = line[0]-'A'; jc = line[1]-'a'; id = line[2]-'A'; jd = line[3]-'a'; if ((ic >= 0) && (ic <= 10) && (jc >= 0) && (jc <= 18) && (current[ic+2][jc+1]==wm) && (dest[id+2][jd+1]==wm-'A'+'a')) { current[id+2][jd+1]=wm; current[ic+2][jc+1]=' '; if (wm=='X') wm = 'O'; else wm = 'X'; draw(current,HORIZONTAL,VERTICAL); start = 1; }; }; } if (len == 5) { putchar(line[0]); putchar(line[1]); putchar(line[2]); putchar(line[3]); putchar(line[4]); printf("%s"," ball to make\n"); copy(dest, current, HORIZONTAL, VERTICAL); play(dest, line, wm, valid, ib, jb, start, HORIZONTAL, VERTICAL); ic = line[0]-'A'; jc = line[1]-'a'; id = line[3]-'A'; jd = line[4]-'a'; if ((valid[0] == 1) && (id >= 0) && (id <= 10) && (jd >= 0) && (jd <= 18) && (dest[id+2][jd+1]=='*')) { current[ic+2][jc+1] = ' '; current[ib+2][jb+1] = wm; current[id+2][jd+1] = '*'; ib = id; jb = jd; start = 1; draw(current,HORIZONTAL,VERTICAL); if ((jb == 0) || (jb == 18)) { if (jb == 0) putchar('O'); else putchar('X'); printf("%s", " WINS!\n"); start = 0; } if (wm=='X') wm = 'O'; else wm = 'X'; }; } printf("Command (h for help): "); } printf("%s","quit\n"); } /* getline: read a line into s, return lenth */ int getline(char s[], int lim) { int c, i; for (i=0; i= 0) && (ic <= 10) && (jc >= 0) && (jc <= 18) && (board[ic+2][jc+1]==current)) { for (hc=-1; hc<=1; hc=hc+1) { for (vc=-1; vc<=1; vc=vc+1) { if ((hc != 0) || (vc != 0)) { i = ic+hc; j = jc+vc; while ((board[i+2][j+1]==' ') && (i >= 0) && (i <= 10) && (j >= 1) && (j <= 17)) { if (current=='X') { board[i+2][j+1]='x'; } if (current=='O') { board[i+2][j+1]='o'; } i = i+hc; j = j+vc; } } } } } } void play(char board[HORIZONTAL][VERTICAL], char line[MAXLINE], char current, int valid[], int ib, int jb, int start, int hor, int ver) { int ic; int jc; int ia; int ja; int i; int j; int ie; int je; int ik; int jk; int h; int v; int empty; int d; int k; int dist; char cur[hor][ver]; char cum[hor][ver][3][3][2]; valid[0] = 0; ic = line[0]-'A'; jc = line[1]-'a'; if ((line[2] == '*') && ((ic == ib) || (jc == jb) || (ic - ib == jc - jb) || (ic - ib == jb - jc)) && (ic >= 0) && (ic <= 10) && (jc >= 1) && (jc <= 17) && (jb >= 1) && (jb <= 17) && (board[ic+2][jc+1]==current)) { empty = 0; if (ic < ib) { ia = 1; dist = ib - ic; } if (ic == ib) { ia = 0; } if (ic > ib) { ia = -1; dist = ic - ib; } if (jc < jb) { ja = 1; dist = jb - jc; } if (jc == jb) { ja = 0; } if (jc > jb) { ja = -1; dist = jc - jb; } i = ic; j = jc; for (d = 1; d < dist; d = d+1) { i = i + ia; j = j + ja; if (board[i+2][j+1] != ' ') { empty = 1; } } if (empty == 0) { valid[0] = 1; board[ic+2][jc+1]=' '; for (h = 0; h <= 2; h=h+1) { for (v = 0; v <= 2; v=v+1) { board[ib+2][jb+1]='*'; copyd(cum, board, hor, ver, h,v,0); board[ib+2][jb+1]=current; copyd(cum, board, hor, ver, h,v,1); }; }; copy(cur, board, hor, ver); k = 0; for (d = 1; d <= dist + dist; d = d+1) { k = 1-k; for (h = 0; h <= 2; h=h+1) { for (v = 0; v <= 2; v=v+1) { if ((h != 1) || (v != 1)) { for (i=2; i <= hor-1; i=i+1) { for (j=2; j <= ver-3;j=j+1) { if (cum[i][j][h][v][1-k] == '*') { ia = i + h - 1; ja = j + v - 1; if ((board[ia][ja] == ' ') && (ia >= 2) & (ia <= hor-1) && (ja >= 2) && (ja <= ver-3)) { cum[ia][ja][h][v][k] = '*'; ie = ia + h - 1; je = ja + v - 1; if ((board[ie][je] != ' ') || (ie < 2) || (ie > hor-1) || (je < 2) || (je > ver-3)) { for (ik = 0; ik <= 2; ik = ik+1) { for (jk = 0; jk <= 2; jk = jk+1) { if ((ik != 1) || (jk != 1)) { cum[ia][ja][ik][jk][k] = '*'; }; }; }; }; }; if ((board[ia][ja] == ' ') && (ia >= 2) & (ia <= hor-1) && (ja >= 1) && (ja <= ver-2) && ((d == dist) || ((start == 1) && (d == (dist+dist))))) { cur[ia][ja] = '*'; }; }; }; }; }; }; }; for (h = 0; h <= 2; h=h+1) { for (v = 0; v <= 2; v=v+1) { copyd(cum, board, hor, ver, h,v,1-k); }; }; }; copy(board,cur,hor,ver); }; }; } void draw(char board[HORIZONTAL][VERTICAL], int h, int v) { int i; int j; for (j=0; j<=VERTICAL-2; j=j+1) { if (j==0) { for (i=1; i<=HORIZONTAL-1; i=i+1) { putchar(board[i][j]); putchar(' '); }; putchar('\n'); putchar(' '); putchar('+'); for (i=2; i<=HORIZONTAL-2; i=i+1) { putchar('-'); putchar('-'); }; putchar('-'); putchar('+'); putchar('\n'); }; if (j==1) { putchar(board[0][1]); putchar('|'); for (i=2; i<=HORIZONTAL-2; i=i+1) { putchar(board[i][j]); putchar(' '); }; putchar(board[HORIZONTAL-1][j]); putchar('|'); putchar('\n'); putchar(' '); putchar('+'); for (i=2; i<=HORIZONTAL-2; i=i+1) { putchar('-'); putchar('+'); }; putchar('-'); putchar('+'); putchar('\n'); }; if ((j >= 2) && (j <= VERTICAL - 3)) { putchar(board[0][j]); putchar('|'); for (i=2; i<=HORIZONTAL-2; i=i+1) { putchar(board[i][j]); putchar('|'); }; putchar(board[HORIZONTAL-1][j]); putchar('|'); putchar('\n'); putchar(' '); putchar('+'); for (i=2; i<=HORIZONTAL-2; i=i+1) { putchar('-'); putchar('+'); }; putchar('-'); putchar('+'); putchar('\n'); }; if (j == VERTICAL -2) { putchar(board[0][VERTICAL-2]); putchar('|'); for (i=2; i<=HORIZONTAL-2; i=i+1) { putchar(board[i][VERTICAL-2]); putchar(' '); }; putchar(board[HORIZONTAL-1][VERTICAL-2]); putchar('|'); putchar('\n'); putchar(' '); putchar('+'); for (i=2; i<=HORIZONTAL-2; i=i+1) { putchar('-'); putchar('-'); }; putchar('-'); putchar('+'); putchar('\n'); }; }; }