/*4il.c (4 in line) - compiled under Borland C++ 5.0. *Version : Beta *Author : Cheok Yan Cheng *Date started coding : 11/10/2000 *Date finished coding : 13/10/2000 */ /*If you had encounted any problem, bugs or comment on this source code *you are welcomed to contact me by my e-mail : yccheok@yahoo.com */ /*Some terms in this source code that you encounted may looked strange to you. *However, I have no time to include the explaination in detail. *If you really want to know, please contact me through e-mail. *Sorry for the inconvenience */ /*Ported to libcaca *Copyright (c) 2009-2010 Sam Hocevar */ #include "config.h" #include #include #include #include "caca_conio.h" #undef max #define X_BOARD 8 #define Y_BOARD 8 #define BOARD_SIZE X_BOARD*Y_BOARD #define BOOLEAN int #define TRUE 1 #define FALSE 0 #define WIN 1 #define LOSE -1 #define DRAW 0 #define OK 2 #define COMPUTER 0 #define HUMAN 1 #define EMPTY -1 #define BASE -2 #define WIN_MESSAGE "I Win! Press Any Key To Continue...\n" #define LOSE_MESSAGE "You Win! Press Any Key To Continue...\n" #define DRAW_MESSAGE "Draw! Press Any Key To Continue...\n" BOOLEAN computerMove(void); BOOLEAN doubleHead(int, int [Y_BOARD][X_BOARD]); BOOLEAN humanMove(int); BOOLEAN isFull(void); BOOLEAN isWon(int, int [Y_BOARD][X_BOARD]); BOOLEAN twoSnake(int, int, int [Y_BOARD][X_BOARD]); int bestmove(void); int max(int [X_BOARD]); int status(void); int xDoubleHead(int, int [Y_BOARD][X_BOARD]); int xTwoSnake(int, int [Y_BOARD][X_BOARD]); int xWon(int, int [Y_BOARD][X_BOARD]); int y_Base(int, int [Y_BOARD][X_BOARD]); void duplicate(int [Y_BOARD][X_BOARD], int [Y_BOARD][X_BOARD]); void drawBoard(void); void drawPiece(void); void genNumWin(int [X_BOARD]); void getHumanMove(void); void init(void); void makeMove(int, int, int [Y_BOARD][X_BOARD]); void sorting(int n[X_BOARD]); int move[X_BOARD] = {3, 4, 2, 5, 1, 6, 0, 7}; int col[X_BOARD] = {-1,-1,-1,-1,-1,-1,-1,-1}; int square[Y_BOARD][X_BOARD] = { {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-2, -2, -2, -2, -2, -2, -2, -2}, }; BOOLEAN computerMove(void) { int x_best; if (isFull()) return FALSE; x_best = bestmove(); gotoxy(52,2); printf("x:%d, y:%d\n" ,x_best+1 ,Y_BOARD-y_Base(x_best,square)); makeMove(COMPUTER, x_best, square); return TRUE; } BOOLEAN doubleHead(int who, int xsquare[Y_BOARD][X_BOARD]) { int x,y; for(y=0; y= X_BOARD) return FALSE; if (y_Base(x, square) == -1) /*indicate no base at that x-coulomb*/ return FALSE; if (isFull()) /*the board is full*/ return FALSE; gotoxy(52,5); printf("x:%d, y:%d\n" ,x+1 ,Y_BOARD-y_Base(x,square)); makeMove(HUMAN,x,square); return TRUE; } BOOLEAN isFull() { int x; for(x=0; x=0 ) return TRUE; } } return FALSE; } BOOLEAN twoSnake(int who, int x, int xsquare[Y_BOARD][X_BOARD]) { int xxsquare[Y_BOARD][X_BOARD]; int n[Y_BOARD] = {0,0,0,0,0,0,0,0}; int i; for(i=0; ibig) big = n[i]; } return big; } int status() { if (isWon(COMPUTER, square)) return WIN; else if (isWon(HUMAN, square)) return LOSE; else if (isFull()) return DRAW; else return OK; } int xDoubleHead(int who, int xsquare[Y_BOARD][X_BOARD]) { int x; int xxsquare[Y_BOARD][X_BOARD]; for(x=0; x0) x--; break; case 77:/*RIGHT*/ if(x<(X_BOARD-1)) x++; break; case 27:/*ESC*/ textcolor(7); textbackground(0); clrscr(); printf("Thank You For Playing 4 in line by Cheok Yan Cheng!\n"); exit(0); break; case 32:/*SPACE*/ if(humanMove(x)) { drawPiece(); return; } else { gotoxy(1,20); textcolor(4); cprintf("OOPs! Wrong Move! \n"); } } } } void init() { int x,y; for(x=0; x0) xsquare[y-1][x] = BASE; } void sorting(int n[]) { int i, j, alpha; int store[X_BOARD]; for(j=0; j