#include <ctype.h>
#include <stdio.h>
#include "graphics.h"
#include "globals.h"
#include "trie.h"

int dx[10] = {0,-1,0,1,-1,0,1,-1,0,1}; 
int dy[10] = {0,1,1,1,0,0,0,-1,-1,-1};

int redrawflag = 1, computermoveflag = 0, gameover = 0;
char state = 0;

#define GO (computermoving=computermoveflag=1)
#undef CTRL
#define CTRL(n) ((n)&31)

void command(void)
{
    char    c;
    int     i,
            j;
    static int nextcom = '~';
    static int nextmovech = '.';
    redrawflag = 1;

 /* Get a command char and clear out lines with temporary messages */
/*    c = getch ();*/
    c = nextcom; if (nextcom == '~') { nextcom = nextmovech;} else { nextcom = '~';}
    clearline (ERRORLINE);
    clearline (MSGLINE);
    if (computermoveflag) {
	computermoveflag = 0;
	zapmove();
    }
     if (state == '#') {
	state = 0;
	if (!isalpha (c))
	    beep ();
	else {
	    c = chr (c) | BLANKBIT;
	    afill[cursory][cursorx] = NEWBIT | c;
	    dfill[cursorx][cursory] = NEWBIT | c;
	    yourrack[BLANK]--;
/*
	    position (cursory, cursorx);
	    standout ();
	    placeletter (c);
	    standend ();
*/
	}
    }

    else
	switch (isupper(c)?tolower (c):c) {	/* Deal with command char */

	    case '7': 
	    case '8': 
	    case '9': 		/* Cursor motion */
	    case '4': 
	    case '6': 
	    case '1': 
	    case '2': 
	    case '3': 
		cursorx += dx[c - '0'];
		cursory += dy[c - '0'];
		redrawflag = 0;
		break;
	    case CTRL('B'):
		cursorx--;
		break;
	    case CTRL('F'):
		cursorx++;
		break;
	    case CTRL('P'):
		cursory--;
		break;
	    case CTRL('N'):
		cursory++;
		break;
	    case 'a': 
	    case 'b': 
	    case 'c': 
	    case 'd': 
	    case 'e': 
	    case 'f': 
	    case 'g': 
	    case 'h': 
	    case 'i': 
	    case 'j': 
	    case 'k': 
	    case 'l': 
	    case 'm': 
	    case 'n': 
	    case 'o': 
	    case 'p': 
	    case 'q': 
	    case 'r': 
	    case 's': 
	    case 't': 
	    case 'u': 
	    case 'v': 
	    case 'w': 
	    case 'x': 
	    case 'y': 
	    case 'z': 
		c = chr (c);
		if (!afill[cursory][cursorx])
		    if (yourrack[c]) {
			afill[cursory][cursorx] = NEWBIT | c;
			dfill[cursorx][cursory] = NEWBIT | c;
			yourrack[c]--;
/*
			position (cursory, cursorx);
			standout ();
			placeletter (c);
			standend ();
*/
		    }
		    else
			if (yourrack[BLANK]) {
			    c = chr (c) | BLANKBIT;
			    afill[cursory][cursorx] = NEWBIT | c;
			    dfill[cursorx][cursory] = NEWBIT | c;
			    yourrack[BLANK]--;
/*
			    tell("(Using your blank)");
			    position (cursory, cursorx);
			    standout ();
			    placeletter (c);
			    standend ();
*/
			}
		    else
		        beep();
		else
		    beep ();
		break;

	    case '#': 
		if (yourrack[BLANK] && !afill[cursory][cursorx]) {
		    state = '#';
		    tell ("Now type the letter you want the blank to be.");
		}
		else
		    beep ();
		break;

	    case ' ': 
		if (afill[cursory][cursorx] & NEWBIT) {
		    c = afill[cursory][cursorx] ^ NEWBIT;
		    if (c & BLANKBIT)
			c = BLANK;
		    yourrack[c]++;
		    afill[cursory][cursorx] = 0;
		    dfill[cursorx][cursory] = 0;
		    position (cursory, cursorx);
		    removeletter ();
		}
		else
		    beep ();
		break;
		
	    case '?':		/* Give new help */
	        givehelp();
		break;

	    case '.': 		/* Make a move */
		if (humanmove (0))
		    GO;
		break;

	    case '~':		/* Suggest a Move */
		for (i = 1; i < 16; i++)
		    for (j = 1; j < 16; j++)
	    		if (afill[i][j] & NEWBIT) {
		    c = afill[i][j] ^ NEWBIT;
		    if (c & BLANKBIT)
			c = BLANK;
		    yourrack[c]++;
		    afill[i][j] = 0;
		    dfill[j][i] = 0;
		    position (i, j);
		    removeletter ();
		}

		if (suggestmove()) {
		    nextmovech = '.';
		} else {
                    nextmovech = '=';
		}
		break;

	    case ':': 
		if (humanmove (1)) {
		    mvprintw (23, 61, "THIS GAME IS BOGUS");
		    GO;
		}
		break;

	    case '-': 		/* exchange tiles */
		consecutivepasses = 0;
		if (exchange ())
		    GO;
		break;

	    case '=': 		/* pass */
		for (i = 1; i < 16; i++)
		    for (j = 1; j < 16; j++)
			if (afill[i][j] & NEWBIT)
			    c = 0;
		if (c) {
		    GO;
		    consecutivepasses++;
		}
		else
		    errortell ("Get your tiles off the board before you pass.");
		break;

	    case CTRL('C'):
	    case '\177':
	    case '+':
		quit ();
		break;

	    case CTRL('Z'):
	        /*tstp ();*/
		break;

	    case CTRL('L'):
/*		wrefresh (curscr);*/
		redrawflag = 0;
		break;

	    default: 
		break;
	}
    if (redrawflag)
	showstuff ();

 /* Remap cursor position into legal range and update the screen */
    cursorx = (cursorx + LEN - 1) % LEN + 1;
    cursory = (cursory + LEN - 1) % LEN + 1;
/*
    position (cursory, cursorx);
    refresh ();
*/
}

void think(void);

void initcommand(void)
{
    cursorx = cursory = 8;
    position (cursory, cursorx);
    showstuff ();
/*
    refresh ();
*/
    think ();
    endgame ();
    quit ();
}

void think(void)
{
    /* change this to execute only one move,
       printboard in .dat format, and exit */

	computermove ();
	showstuff ();
}