Ejemplo n.º 1
0
static void
getevent(void)			/* get next event */
{
	XNextEvent(ourdisplay, levptr(XEvent));
	switch (levptr(XEvent)->type) {
	case ConfigureNotify:
		resizewindow(levptr(XConfigureEvent));
		break;
	case UnmapNotify:
		mapped = 0;
		break;
	case MapNotify:
		mapped = 1;
		break;
	case Expose:
		fixwindow(levptr(XExposeEvent));
		break;
	case KeyPress:
		getkey(levptr(XKeyPressedEvent));
		break;
	case ButtonPress:
		switch (levptr(XButtonPressedEvent)->button) {
		case Button4:		/* wheel up */
		case Button5:		/* wheel down */
			break;
		default:
			getmove(levptr(XButtonPressedEvent));
			break;
		}
		break;
	}
}
Ejemplo n.º 2
0
int main() {
  
    int gameover = 0; // used to check if the game is over or not
  
    char uncovered_board[SIDE][SIDE]; // board that stores the location of all
                                      // the bombs and numbers of adjacent bombs.

    char current_board[SIDE][SIDE]; // stores the current board as it should be
                                    // printed out.

    int totalmoves; // stores the total moves left for the user before they win.

    int i,j;
    int x,y;

    int listbombs[NUMBOMBS][2]; // stores (x,y) coordinates of all bombs.
  
    srand(time(0)); // Seed the random number generator.

    // Initialize the current board.
    for (i=0;i<SIDE;i++) {
        for (j=0;j<SIDE;j++) {
            current_board[i][j] = '_';
            uncovered_board[i][j] = '_';
        }
    }

    // Randomly choose where the bombs will go.
    getbombs(listbombs);
  
    // Place the bombs onto the board.
    for (i=0;i<NUMBOMBS;i++) 
        uncovered_board[listbombs[i][0]][listbombs[i][1]]='*';
  
    totalmoves = SIDE*SIDE - NUMBOMBS;

    // Continue playing until the game ends.
    while (!gameover) {
        
        // Get a move from the user.
        printf("Here is the board : \n");
        printboard(current_board);
        getmove(&x,&y);
        printf("at 7 1 when get move is %c\n", current_board[7][1]);
    
        // Execute that move.
        gameover = domove(current_board,uncovered_board,listbombs,x,y,&totalmoves);

        // Check if they have won.
        if ((!gameover) && (totalmoves==0)) {
            printf("Great! You win!!!\n");
            gameover = 1;
        }
    }
  
    system("PAUSE");
    return 0;
  
}
Ejemplo n.º 3
0
Archivo: nmm.c Proyecto: ryanakca/nmm
/*
 * Handle a mill by dealing with the removal of an opponent's piece.
 * Prompts the user for a piece to remove, checks its legality, and
 * removes it. Return the removed piece, recursing until a piece is
 * removed.
 */
point *
mill_handler(scrgame *sg, char *move, int count)
{
  point *p = NULL;

  if (count == 0) {
    update_msgbox(sg->msg_w,
		  "You've formed a mill, enter opponent piece to remove.");
  }
  getmove(sg, move, 3);
  if ((p = getpoint(sg->game, move))) {
    if (p->v != statechar(sg->game) && p->v != EMPTY) {
      if (inmill(p)) {
	/* We can only break an opponent's mill if there are no other
	   pieces to remove. We could keep a list of each player's
	   pieces, but this implies the headache of always updating
	   it. Alternatively, we can just iterate through at most 21
	   points, until we find an opponent piece not in a mill, or
	   nothing. */
	int r = 0;
	int c = 0;
	int opp = 0;
	if (sg->game->state == WHITE) {
	  opp = BLACKC;
	} else {
	  opp = WHITEC;
	}
	for (r = 0; r < 3; r++) {
	  for (c = 0; c < 7; c++) {
	    if (sg->game->board[r][c]->v == opp && !inmill(sg->game->board[r][c])) {
	      update_msgbox(sg->msg_w,
			    "It is possible to remove a piece not in a mill; do so.");
	      return mill_handler(sg, move, 1);
	    }
	  }
	}
	/* By this point, we've iterated through all of the opponent's
	   pieces without encountering one not in a mill. Allow the
	   desired piece to be removed. */
      }
      p->v = EMPTY;
      /* Decrement the opponent's pieces */
      sg->game->pieces[sg->game->state ^ BLACK]--;
      return p;
    } else if (p->v == EMPTY) {
      update_msgbox(sg->msg_w,
		    "You tried clearing an empty position. Please try again.");
      return mill_handler(sg, move, 1);
    } else {
      update_msgbox(sg->msg_w,
		    "You tried removing your own piece. Please try again.");
      return mill_handler(sg, move, 1);
    }
  } else {
    update_msgbox(sg->msg_w, "Invalid coordinates. Please try again.");
    return mill_handler(sg, move, 1);
  }
}
Ejemplo n.º 4
0
void getmove(char move[], int *i, int *j)
/* interpret response of human move to board position */
  {
   FILE *fp;
   int m, n;

   if (strcmp(move, "stop") == 0)
/* stop game */
      play = 0;
   else
     {
      if (strcmp(move, "save") == 0)
/* save data and stop game */
	{
	 fp = fopen("gnugo.dat", "w");
/* save board configuration */
	 for (m = 0; m < 19; m++)
	   for (n = 0; n < 19; n++)
	       fprintf(fp, "%c", p[m][n]);
/* my color, pieces captured */
         fprintf(fp, "%d %d %d ", mymove, mk, uk);
/* opening pattern flags */
         for (m = 0; m < 9; m++)
           fprintf(fp, "%d ", opn[m]);

	 fclose(fp);
	 play = -1;
       }
      else
	{
	 if (strcmp(move, "pass") == 0)
/* human pass */
	   {
	    pass++;
	    *i = -1;   /* signal pass */
	  }
	 else
	   {
	    pass = 0;
/* move[0] from A to T, move[1] move[2] from 1 to 19 */
/* convert move to coordinate */
	    if (!getij(move, i, j) || (p[*i][*j] != EMPTY) || suicide(*i, *j))
	      {
               if (feof(stdin)) exit(1);
	       printf("illegal move !\n");
	       printf("your move? ");
	       scanf("%s", move);
	       getmove(move, i, j);
	     }
	 }
       }
    }
}  /* end getmove */
Ejemplo n.º 5
0
Archivo: nmm.c Proyecto: ryanakca/nmm
/*
 * Phases two and three of the game
 */
point *
phasetwothree(scrgame *sg)
{
  point *p = NULL;
  char coords[6];
  while (sg->game->pieces[WHITE] >= 3 && sg->game->pieces[BLACK] >= 3) {
    if (sg->game->pieces[sg->game->state] > 3 && surrounded(sg->game)) {
      update_scorebox(sg->score_w, sg->game);
      update_board(sg->board_w, sg->game);
      return NULL;
    }
    getmove(sg, coords, 0);
    if (!(p = getpoint(sg->game, coords))) {
      update_msgbox(sg->msg_w, "Something went wrong...");
      continue;
    }
    if (p->v != statechar(sg->game)) {
      update_msgbox(sg->msg_w, "Please move your own piece.");
      continue;
    }
    if (sg->game->pieces[sg->game->state] == 3) {
      if (!(p = jumppiece(p, sg->game, &coords[2]))) {
	update_msgbox(sg->msg_w,
		      "That location is already occupied. Please try again");
	continue;
      }
    } else if (!(p = movepiece(p, &coords[2]))) {
      update_msgbox(sg->msg_w,
		    "That location is already occupied. Please try again");
      continue;
    }
    if (inmill(p)) {
      /* We should redraw the board now so that the player can see the
	 piece he just played */
      update_scorebox(sg->score_w, sg->game);
      update_board(sg->board_w, sg->game);
      update_msgbox(sg->msg_w, "");
      mill_handler(sg, coords, 0);
    }
    sg->game->state ^= BLACK;
    if (sg->game->pieces[BLACK] == 3 || sg->game->pieces[WHITE] == 3) {
      sg->game->phase = 3;
    }
    update_scorebox(sg->score_w, sg->game);
    update_board(sg->board_w, sg->game);
    update_msgbox(sg->msg_w, "");
  }
  return p;
}
Ejemplo n.º 6
0
Archivo: nmm.c Proyecto: ryanakca/nmm
/*
 * Phase one of the game
 * Returns NULL if one player is guaranteed to have less than 3 pieces
 * at the end of the phase, thus loosing.
 */
point *
phaseone(scrgame *sg)
{
  point *p = NULL;
  char coords[4];
  int maxrem = 0;
  for (; sg->game->totalpieces < 2 * sg->game->type; sg->game->totalpieces++) {
    getmove(sg, coords, 0);
    if (!(p = placepiece(sg, coords))) {
      sg->game->totalpieces--;
      continue;
    }
    sg->game->pieces[sg->game->state]++;
    if (inmill(p)) {
      /* We should redraw the board now so that the player can see the
	 piece he just played */
      update_scorebox(sg->score_w, sg->game);
      update_board(sg->board_w, sg->game);
      update_msgbox(sg->msg_w, "");
      mill_handler(sg, coords, 0);
    }
    sg->game->state ^= BLACK;
    update_scorebox(sg->score_w, sg->game);
    update_board(sg->board_w, sg->game);
    update_msgbox(sg->msg_w, "");
    maxrem = (2 * sg->game->type - sg->game->totalpieces) / 2;
    if (sg->game->pieces[0] + maxrem < 3 ||
	sg->game->pieces[1] + maxrem < 3) {
      /* It's impossible for a player to place more than 3 pieces, abort */
      return NULL;
    }
  }
  if (sg->game->type == TMM) {
    /* Three Man Morris doesn't have a phase 2 */
    sg->game->phase = 3;
  } else {
    sg->game->phase = 2;
  }
  update_scorebox(sg->score_w, sg->game);
  return p;
}
Ejemplo n.º 7
0
static int
dev_input(		/* get next input event */
	int	nsecs
)
{
#if 0
	static time_t	lasttime = 0;
	time_t	thistime;

	if (nsecs > 0) {
		thistime = time(0);
		nsecs -= (long)(thistime - lasttime);
		lasttime = thistime;
	}
	if (nsecs > 0)
		alarm(nsecs);
#endif
	XNextEvent(ourdisplay, levptr(XEvent));
	switch (levptr(XEvent)->type) {
	case ConfigureNotify:
		resizewindow(levptr(XConfigureEvent));
		break;
	case UnmapNotify:
		mapped = 0;
		break;
	case MapNotify:
		mapped = 1;
		break;
	case Expose:
		fixwindow(levptr(XExposeEvent));
		break;
	case KeyPress:
		return(getkey(levptr(XKeyPressedEvent)));
	case ButtonPress:
		getmove(levptr(XButtonPressedEvent));
		break;
	}
	return(1);
}
Ejemplo n.º 8
0
int
main (int argc, char **argv)
{
	int     i,l;		/* non-descript indices */
	char    c;		/* non-descript character storage */

	if (pledge("stdio rpath wpath cpath tty exec", NULL) == -1)
		err(1, "pledge");

	signal(SIGINT, getout);	/* trap interrupts */

	/* use whole screen for text */
	begscr = 0;

	getarg(argc, argv);

	initcurses();

	/* check if restored game and save flag for later */
	if ((rfl = rflag)) {
		if (pledge("stdio rpath wpath cpath tty", NULL) == -1)
			err(1, "pledge");

		wrboard();	/* print board */
		/* if new game, pretend to be a non-restored game */
		if (cturn == 0)
			rflag = 0;
	} else {
		rscore = wscore = 0;	/* zero score */

		if (aflag) {	/* print rules */
			addstr(rules);
			if (yorn(0)) {
				endwin();
				execl(TEACH, "teachgammon", (char *)NULL);

				err(1, "%s", noteach);
			} else {/* if not rules, then instructions */
				addstr(need);
				if (yorn(0)) {	/* print instructions */
					clear();
					text(instruct);
				}
			}
		}

		if (pledge("stdio rpath wpath cpath tty", NULL) == -1)
			err(1, "pledge");

		init();		/* initialize board */

		if (pnum == 2) {/* ask for color(s) */
			printw("\n%s", askcol);
			while (pnum == 2) {
				c = readc();
				switch (c) {

				case 'R':	/* red */
					pnum = -1;
					break;

				case 'W':	/* white */
					pnum = 1;
					break;

				case 'B':	/* both */
					pnum = 0;
					break;

				case 'P':	/* Control the dice */
					iroll = 1;
					addstr("\nDice controlled!\n");
					addstr(askcol);
					break;

				default:	/* error */
					beep();
				}
			}
		}

		wrboard();		/* print board */

		move(18, 0);
	}
	/* limit text to bottom of screen */
	begscr = 17;

	for (;;)  {			/* begin game! */
		/* initial roll if needed */
		if ((!rflag) || raflag)
			roll();

		/* perform ritual of first roll */
		if (!rflag) {
			move(17, 0);
			while (D0 == D1)	/* no doubles */
				roll();

			/* print rolls */
			printw("%s%d%s%d", rollr, D0, rollw, D1);

			/* winner goes first */
			if (D0 > D1) {
				addstr(rstart);
				cturn = 1;
			} else {
				addstr(wstart);
				cturn = -1;
			}
		}
		/* initialize variables according to whose turn it is */

		if (cturn == 1) {	/* red */
			home = 25;
			bar = 0;
			inptr = &in[1];
			inopp = &in[0];
			offptr = &off[1];
			offopp = &off[0];
			Colorptr = &color[1];
			colorptr = &color[3];
			colen = 3;
		} else {		/* white */
			home = 0;
			bar = 25;
			inptr = &in[0];
			inopp = &in[1];
			offptr = &off[0];
			offopp = &off[1];
			Colorptr = &color[0];
			colorptr = &color[2];
			colen = 5;
		}

		/* do first move (special case) */
		if (!(rflag && raflag)) {
			if (cturn == pnum)	/* computer's move */
				domove(0);
			else {	/* player's move */
				mvlim = movallow();
				/* reprint roll */
				move(cturn == -1 ? 18 : 19, 0);
				proll();
				getmove();	/* get player's move */
			}
		}
		move(17, 0);
		clrtoeol();
		begscr = 18;
		/* no longer any difference between normal and recovered game. */
		rflag = 0;

		/* move as long as it's someone's turn */
		while (cturn == 1 || cturn == -1) {

			/* board maintainence */
			moveplayers();	/* fix board */

			/* do computer's move */
			if (cturn == pnum) {
				domove(1);

				/* see if double refused */
				if (cturn == -2 || cturn == 2)
					break;

				/* check for winning move */
				if (*offopp == 15) {
					cturn *= -2;
					break;
				}
				continue;

			}
			/* (player's move) */

			/* clean screen if safe */
			if (hflag) {
				move(20, 0);
				clrtobot();
				hflag = 1;
			}
			/* if allowed, give him a chance to double */
			if (dflag && dlast != cturn && gvalue < 64) {
				move(cturn == -1 ? 18: 19, 0);
				addstr(*Colorptr);
				c = readc();

				/* character cases */
				switch (c) {

				case 'R':		/* reprint board */
					wrboard();
					break;

				case 'S':		/* save game */
					raflag = 1;
					save(1);
					break;

				case 'Q':		/* quit */
					quit();
					break;

				case 'D':		/* double */
					dble();
					break;

				case ' ':		/* roll */
				case '\n':
					roll();
					printw(" rolls %d %d.  ", D0, D1);

					/* see if he can move */
					if ((mvlim = movallow()) == 0) {

						/* can't move */
						printw("%s%s%s", toobad1, *colorptr, unable);
						if (pnum) {
							moveplayers();
							sleep(MVPAUSE);
						}
						nexturn();
						break;
					}

					getmove();

					/* okay to clean screen */
					hflag = 1;
					break;

				default:		/* invalid character */

					/* print help message */
					move(20, 0);
					text(helpm);
					move(cturn == -1 ? 18 : 19, 0);

					/* don't erase */
					hflag = 0;
				}
			} else {/* couldn't double */

				/* print roll */
				roll();
				move(cturn == -1 ? 18: 19, 0);
				proll();

				/* can he move? */
				if ((mvlim = movallow()) == 0) {

					/* he can't */
					printw("%s%s%s", toobad2, *colorptr, cantmv);
					moveplayers();
					sleep(MVPAUSE);
					nexturn();
					continue;
				}

				getmove();
			}
		}

		/* don't worry about who won if quit */
		if (cturn == 0)
			break;

		/* fix cturn = winner */
		cturn /= -2;

		/* final board pos. */
		moveplayers();

		/* backgammon? */
		mflag = 0;
		l = bar + 7 * cturn;
		for (i = bar; i != l; i += cturn)
			if (board[i] * cturn)
				mflag++;

		/* compute game value */
		move(20, 0);
		if (*offopp == 15) {
			if (mflag) {
				addstr(bgammon);
				gvalue *= 3;
			}
			else if (*offptr <= 0) {
				addstr(gammon);
				gvalue *= 2;
			}
		}
		/* report situation */
		if (cturn == -1) {
			addstr("Red wins ");
			rscore += gvalue;
		} else {
			addstr("White wins ");
			wscore += gvalue;
		}
		printw("%d point%s.\n", gvalue, (gvalue > 1) ? "s":"");

		/* write score */
		wrscore();

		/* see if he wants another game */
		addstr(again);
		if ((i = yorn('S')) == 0)
			break;

		init();
		if (i == 2) {
			addstr("  Save.\n");
			cturn = 0;
			save(0);
		}
		/* yes, reset game */
		wrboard();
	}

	/* give him a chance to save if game was recovered */
	if (rfl && cturn) {
		addstr(svpromt);
		if (yorn(0)) {
			/* re-initialize for recovery */
			init();
			cturn = 0;
			save(0);
		}
	}
	/* leave peacefully */
	getout(0);
	/* NOT REACHED */
}
Ejemplo n.º 9
0
Archivo: nmm.c Proyecto: ryanakca/nmm
/*
 * Prompts the user for a move with getinput, sanitizes it, and makes
 * the move. It stores the move in *move, you can specify the maximum
 * length l of the move to take, this should be at most the length of
 * move. If mill is less than three or more than five, move should
 * hold at least 5 characters. Returns pointer to the move.
 */
char *
getmove(scrgame *sg, char *move, int l)
{
  int length, pieces, index, twmm = 0;
  /* Do we only have 3 pieces, i.e. are we in skipping mode? */
  pieces = sg->game->pieces[(int) sg->game->state];
  if (l < 3 || 5 < l) {
    if (sg->game->type == TWMM) {
      /* We need to add one to our length to make up for the
       * possibility of ne/nw/se/sw over n/e/s/w */
      twmm = 1;
    }
    switch (sg->game->phase)
    {
    case 1:
      /* In piece placing mode */
      length = 3;
      break;

    case 2:
      /* Of form d3s or d3sw */
      length = 4 + twmm;
      break;

    case 3:
      if (pieces == 3) {
	/* Of form d3a1 */
	length = 5;
      } else {
	/* Of form d3s or d3sw */
	length = 4 + twmm;
      }
      break;

    default:
      length = 0;
      /* NOTREACHED */
    }
  } else {
    length = l;
  }
  move = getinput(sg, move, length);
  move = lower(move);
  if (!validcoords(sg->game, move) ||
      /* Check the length to make sure we're not in mill mode */
      (pieces == 3 && length != 3 && !validcoords(sg->game, &move[2]))) {
    update_msgbox(sg->msg_w, "Invalid coordinates");
    return getmove(sg, move, length);
  }
  /* If we'e in piece sliding stage. The length check makes sure we're
     not in mill mode. */
  if (sg->game->phase != 1 && pieces != 3 && length != 3) {
    index = dirtoindex(&move[2]);
    if (index == -1) {
      update_msgbox(sg->msg_w, "Invalid direction");
      return getmove(sg, move, length);
    } else if (!(getpoint(sg->game, /* We already checked that move[2]
				       is safe */
			  move)->n[index])) {
      update_msgbox(sg->msg_w, "Impossible to move in that direction");
      return getmove(sg, move, length);
    }
  }
  move[length - 1] = '\0';
  return move;
}
Ejemplo n.º 10
0
tutor ()  {
	register int	i, j;

	i = 0;
	begscr = 18;
	cturn = -1;
	home = 0;
	bar = 25;
	inptr = &in[0];
	inopp = &in[1];
	offptr = &off[0];
	offopp = &off[1];
	Colorptr = &color[0];
	colorptr = &color[2];
	colen = 5;
	wrboard();

	while (1)  {
		if (! brdeq(test[i].brd,board))  {
			if (tflag && curr == 23)
				curmove (18,0);
			writel (better);
			nexturn();
			movback (mvlim);
			if (tflag)  {
				refresh();
				clrest ();
			}
			if ((! tflag) || curr == 19)  {
				proll();
				writec ('\t');
			}
			else
				curmove (curr > 19? curr-2: curr+4,25);
			getmove();
			if (cturn == 0)
				leave();
			continue;
		}
		if (tflag)
			curmove (18,0);
		text (*test[i].com);
		if (! tflag)
			writec ('\n');
		if (i == maxmoves)
			break;
		D0 = test[i].roll1;
		D1 = test[i].roll2;
		d0 = 0;
		mvlim = 0;
		for (j = 0; j < 4; j++)  {
			if (test[i].mp[j] == test[i].mg[j])
				break;
			p[j] = test[i].mp[j];
			g[j] = test[i].mg[j];
			mvlim++;
		}
		if (mvlim)
			for (j = 0; j < mvlim; j++)
				if (makmove(j))
					writel ("AARGH!!!\n");
		if (tflag)
			refresh();
		nexturn();
		D0 = test[i].new1;
		D1 = test[i].new2;
		d0 = 0;
		i++;
		mvlim = movallow();
		if (mvlim)  {
			if (tflag)
				clrest();
			proll();
			writec('\t');
			getmove();
			if (tflag)
				refresh();
			if (cturn == 0)
				leave();
		}
	}
	leave();
}
Ejemplo n.º 11
0
void
domove()
{
	PLAY	*pp;
	int	i, j;
	bool	goodplay;

	pp = &Player[Play];
	if (Play == PLAYER)
		getmove();
	else
		calcmove();
	Next = FALSE;
	goodplay = TRUE;
	switch (Movetype) {
	  case M_DISCARD:
		if (haspicked(pp)) {
			if (pp->hand[Card_no] == C_INIT) {
				if (Card_no == 6)
					Finished = TRUE;
				else
					error("no card there");
			} else {
				if (is_safety(pp->hand[Card_no])) {
					error("discard a safety?");
					goodplay = FALSE;
					break;
				}
				Discard = pp->hand[Card_no];
				pp->hand[Card_no] = C_INIT;
				Next = TRUE;
				if (Play == PLAYER)
					account(Discard);
			}
		}
		else
			error("must pick first");
		break;
	  case M_PLAY:
		goodplay = playcard(pp);
		break;
	  case M_DRAW:
		Card_no = 0;
		if (Topcard <= Deck)
			error("no more cards");
		else if (haspicked(pp))
			error("already picked");
		else {
			pp->hand[0] = *--Topcard;
#ifdef DEBUG
			if (Debug)
				fprintf(outf, "DOMOVE: Draw %s\n", C_name[*Topcard]);
#endif
acc:
			if (Play == COMP) {
				account(*Topcard);
				if (is_safety(*Topcard))
					pp->safety[*Topcard-S_CONV] = S_IN_HAND;
			}
			if (pp->hand[1] == C_INIT && Topcard > Deck) {
				Card_no = 1;
				pp->hand[1] = *--Topcard;
#ifdef DEBUG
				if (Debug)
					fprintf(outf, "DOMOVE: Draw %s\n", C_name[*Topcard]);
#endif
				goto acc;
			}
			pp->new_battle = FALSE;
			pp->new_speed = FALSE;
		}
		break;

	  case M_ORDER:
		break;
	}
	/*
	 * move blank card to top by one of two methods.  If the
	 * computer's hand was sorted, the randomness for picking
	 * between equally valued cards would be lost
	 */
	if (Order && Movetype != M_DRAW && goodplay && pp == &Player[PLAYER])
		sort(pp->hand);
	else
		for (i = 1; i < HAND_SZ; i++)
			if (pp->hand[i] == C_INIT) {
				for (j = 0; (j < HAND_SZ) &&
					 (pp->hand[j] == C_INIT); j++)
					;	
				if (j == HAND_SZ)
					j = 0;
				pp->hand[i] = pp->hand[j];
				pp->hand[j] = C_INIT;
			}
	if (Topcard <= Deck)
		check_go();
	if (Next)
		nextplay();
}
Ejemplo n.º 12
0
void imcsplay(int argc, char **argv) {
    setlinebuf(stdout);
    if (argc < 5 || argc > 7)
	usage();
    char mecolor = '?';
    int megame = 0;
    switch(argv[2][0]) {
    case 'O':
	switch(argv[2][1]) {
	case 'W':
	case 'B':
	case '?':
	    mecolor = argv[2][1];
	    break;
	default:
	    usage();
	}
	break;
    case 'A': {
	char ch = argv[2][1];
	if (isdigit(ch)) {
	    megame = atoi(&argv[2][1]);
	} else if (ch == 'W' || ch == 'B') {
	    mecolor = ch;
	    megame = atoi(&argv[2][2]);
	} else {
	    usage();
	}
	if (megame <= 0)
	    usage();
	break;
    }
    default:
	usage();
    }
    char *meuser = argv[3];
    char *mepassword = argv[4];
    char *host = "imcs.svcs.cs.pdx.edu";
    if (argc > 5)
	host = argv[5];
    int port = 3589;
    if (argc > 6) {
	port = atoi(argv[6]);
	if (port <= 0)
	    usage();
    }
    FILE *nf = netopen(host, port);
    setlinebuf(nf);
    startlog();
    char *greeting = expectcmd(nf, 1, 100, 0);
    (void) strtok(greeting, " ");
    char *pgm = strtok(0, " ");
    assert(!strcmp(pgm, "imcs"));
    char *version = strtok(0, " \r\n");
    if(strcmp(version, "2.5")) {
	fprintf(stderr, "got unexpected imcs version %s\n", version);
	exit(1);
    }
    sendcmd(nf, "me %s %s", meuser, mepassword);
    (void) expectcmd(nf, 1, 201, 0);
    if (megame != 0) {
	if (mecolor == '?')
	    sendcmd(nf, "accept %d", megame);
	else
	    sendcmd(nf, "accept %d %c", megame, mecolor);
	(void) expectcmd(nf, 1, 105, 106, 0);
    } else {
	if (mecolor == '?')
	    sendcmd(nf, "offer");
	else
	    sendcmd(nf, "offer %c", mecolor);
	(void) expectcmd(nf, 1, 103, 0);
	logmsg("waiting for opponent");
	(void) expectcmd(nf, 1, 105, 106, 0);
	logmsg("opponent found");
    }
    struct state s = s0;
    s.cureval = eval(&s);
    if (nttable > 0)
	s.curzhash = zhash(&s);
    while (1) {
	int ch = fgetc(nf);
	int r = ungetc(ch, nf);
	assert(r != EOF);
	if (isdigit(ch)) {
	    s = readstate(nf, 1);
	    s.cureval = eval(&s);
	    if (nttable > 0)
		s.curzhash = zhash(&s);
	    continue;
	}
	switch (ch) {
	case '?': {
	    char *r = getnet(nf, "?");
	    char *q = strtok(r, " ");
	    assert(!strcmp(q, "?"));
	    char *tl = strtok(0, " ");
	    char *tr = strtok(0, " ");
	    assert(tl && tr);
	    int t = readtimems(tl);
	    t = 95 * t / (100 * ((81 - s.ply) / 2));
	    struct move m = idnegamax(&s, t, 0);
	    logmsg("value %d at time %d depth %d for %s\n\n",
		   v0, t, d0, movestr(&m));
	    move(&s, &m, 0);
	    sendcmd(nf, "%s", movestr(&m));
	    printstate(&s, 1);
	    if (ponder)
		(void) idnegamax(&s, 0, nf);
	    continue;
	}
	case '!':
	    assert(fgetc(nf) == '!');
	    int ch;
	    do
		ch = fgetc(nf);
	    while (isspace(ch));
	    ungetc(ch, nf);
	    struct move m = getmove(nf, &s);
	    move(&s, &m, 0);
	    continue;
	case '=':
	    (void) getnet(nf, "=");
	    break;
	case 'X':
	    (void) getnet(nf, "X");
	    break;
	default:
	    (void) getnet(nf, "...");
	    continue;
	}
	break;
    }
    fclose(nf);
}
Ejemplo n.º 13
0
void
tutor(void)
{
	int     i, j, k;
	int     wrongans;

	wrongans = 0;
	i = 0;
	begscr = 18;
	cturn = -1;
	home = 0;
	bar = 25;
	inptr = &in[0];
	inopp = &in[1];
	offptr = &off[0];
	offopp = &off[1];
	Colorptr = &color[0];
	colorptr = &color[2];
	colen = 5;
	wrboard();

	while (1) {
		if (!brdeq(test[i].brd, board)) {
			wrongans++;
			move(18, 0);
			if (wrongans >= 3) {
				wrongans = 0;
				text(*test[i].ans);
				memcpy(board,test[i].brd,26*sizeof(int));
				/* and have to fix *inptr, *offptr; player is red (+ve) */
				k = 0;
				for (j = 19; j < 26; j++)
					k += (board[j] > 0 ? board[j] : 0);
				*inopp = k;
				for (j = 0; j < 19; j++)
					k += (board[j] > 0 ? board[j] : 0);
				*offopp = k - 30;  /* -15 at start */
				moveplayers();
				clrest();
			} else {
				addstr(better);
				nexturn();
				movback(mvlim);
				moveplayers();
				clrest();
				getyx(stdscr, j, k);
				if (j == 19) {
					proll();
					addch('\t');
				} else
					move(j > 19 ? j - 2 : j + 4, 25);
				getmove();
				if (cturn == 0)
					leave();
				continue;
			}
		} else
			wrongans = 0;
		move(18, 0);
		text(*test[i].com);
		move(19, 0);
		if (i == maxmoves)
			break;
		D0 = test[i].roll1;
		D1 = test[i].roll2;
		d0 = 0;
		mvlim = 0;
		for (j = 0; j < 4; j++) {
			if (test[i].mp[j] == test[i].mg[j])
				break;
			p[j] = test[i].mp[j];
			g[j] = test[i].mg[j];
			mvlim++;
		}
		if (mvlim)
			for (j = 0; j < mvlim; j++)
				if (makmove(j))
					addstr("AARGH!!!\n");
		moveplayers();
		nexturn();
		D0 = test[i].new1;
		D1 = test[i].new2;
		d0 = 0;
		i++;
		mvlim = movallow();
		if (mvlim) {
			clrest();
			proll();
			addch('\t');
			getmove();
			moveplayers();
			if (cturn == 0)
				leave();
		}
	}
	leave();
}