void cal_roundCost(Location cur)
{
	Location buf[6], buf2[6];
	int i, j;
	
	get_round(cur, buf);
	for(i = 0; i < 6; i++)
		map[buf[i].x][buf[i].y].cost = 1;

	for(i = 0; i < 6; i++) {
		if(map[buf[i].x][buf[i].y].type != WAY) {
			continue;
		}
		else {
			get_round(buf[i], buf2); //note!
			for(j = 0; j < 6; j++) {
				if(map[buf2[j].x][buf2[j].y].type == WAY)
					map[buf[i].x][buf[i].y].cost++;
			}
		}
	}
}
int max_cost(Location cur)
{
	Location buf[6];
	int i, nextStep, maxCost = 0;

	cal_roundCost(cur);
	get_round(cur, buf);
	for(i = 0; i < 6; i++) 
		if((map[buf[i].x][buf[i].y].type == WAY) && 
				(maxCost < map[buf[i].x][buf[i].y].cost)) {
			maxCost = map[buf[i].x][buf[i].y].cost;
			nextStep = i;
		}
	if(maxCost == 0)
		who_win(PLAYER);

	return nextStep;
}
int min_path(Location cur)
{
	Location buf[6];
	int next_step = 0;
	int minPath;
	int i;

	cal_allPath();
	get_round(cur, buf);
	minPath = 100;
	for(i = 0; i < 6; i++) {
		if(buf[i].path < minPath) {
			next_step = i;
			minPath = buf[i].path;
		}
	}

	if(minPath == 100)
		who_win(PLAYER);

	return next_step;
}
void cal_onePath(Location *cur)
{
	int min, i;
	Location buf[6];

	if(cur->type == STONE)
		min = 100;
	 else {
		 if(is_boundary((*cur)))
			min =  0;
		else {
			get_round((*cur), buf);
			min = 100;
			for(i = 0; i < 6; i++) {
				if(min > abs(buf[i].path)) {
					min = buf[i].path;				
				}
			}
			if(min != 100)
				min++;
		}
	 }
	cur->path = min;
}
void cal_allOut()
{
	int i, j, k, l, end;
	Location cur, buf[6];

	//init all out
	for(i = 0; i < 9; i++) {
		for(j = 0; j < 9; j++) {
			map[i][j].out = -1;
		}
	}

	//init boundary out
	for(i = 0; i < 9; i++) {
		if(map[0][i].type != STONE)
			map[0][i].out = 0;
		if(map[8][i].type != STONE)
			map[8][i].out = 0;
		if(map[i][0].type != STONE)
			map[i][0].out = 0;
		if(map[i][8].type != STONE)
			map[i][8].out = 0;
	}
/*
	for(i = 1, j = 1, end = 7; i <= 4; i++, j++, end -= 2) {
		for(k = 0; k < end; k++) {
			if(map[i+k][j+k].type != STONE) {
				get_round(map[i+k][j+k], buf);
				for(l = 0; l < 6; l++) {
					if(map[buf[l].x][buf[l].y].out == 0) {
						map[i+k][j+k].out = 0;
						break;
					}
				}
			}
		}
	}
	for(i = 7, j = 7, end = 7; i >= 4; i--, j--, end -= 2) {
		for(k = 0; k < end; k++) {
			if(map[i-k][j-k].type != STONE) {
				get_round(map[i-k][j-k], buf);
				for(l = 0; l < 6; l++) {
					if(map[buf[l].x][buf[l].y].out == 0) {
						map[i-k][j-k].out = 0;
						break;
					}
				}
			}
		}
	}
	
*/
	//init other, (1,1)->(7,7)
	for(i = 1; i < 8; i++) {
		for(j = 1; j < 8; j++ ) {
			if(map[i][j].type != STONE) {
				get_round(map[i][j], buf);
				for(k = 0; k < 6; k++) {
					if(map[buf[k].x][buf[k].y].out == 0) {
						map[i][j].out = 0;
						break;
					}
				}									
			}
		}
	}

	//init other, (7,7)->(1,1)
	for(i = 7; i >= 0; i--) {
		for(j = 7; j >= 0; j--) {
			if((map[i][j].type != STONE) && (map[i][j].out == -1)) {
				get_round(map[i][j], buf);
				for(k = 0; k < 6; k++) {
					if(map[buf[k].x][buf[k].y].out == 0) {
						map[i][j].out = 0;
						break;
					}
				}
			}
		}
	}
	//init other, (7,1)->(1,7)
	for(i = 7; i >= 0; i--) {
		for(j = 1; j < 8; j++) {
			if((map[i][j].type != STONE) && (map[i][j].out == -1)) {
				get_round(map[i][j], buf);
				for(k = 0; k < 6; k++) {
					if(map[buf[k].x][buf[k].y].out == 0) {
						map[i][j].out = 0;
						break;
					}
				}
			}
		}
	}
	//init other, (1,7)->(7,1)
	for(i = 1; i < 8; i++) {
		for(j = 7; j >= 0; j--) {
			if((map[i][j].type != STONE) && (map[i][j].out == -1)) {
				get_round(map[i][j], buf);
				for(k = 0; k < 6; k++) {
					if(map[buf[k].x][buf[k].y].out == 0) {
						map[i][j].out = 0;
						break;
					}
				}
			}
		}
	}

}
int NEAR PASCAL CommandHandler(WORD wParam, LONG lParam)
{
/*  if (HIWORD(lParam) == EN_CHANGE)
  {
    if (focus(GetDlgCtrlID(LOWORD(lParam))) && players.player_numb > -1)
      bNeedSaveP = TRUE;
    else if (courses.course_numb > -1)
      bNeedSaveC = TRUE;
  }
*/
  switch (wParam)
  {
    case IDOK:
      SendMessage(hWnd, WM_COMMAND, IDD_RLIST, MAKELONG(0, LBN_DBLCLK));
      return NULL;

    case IDM_CONTENTS:
    case IDM_SEARCHON:
    case IDM_USE:
      MessageBox(hWnd, "Help to come soon!", szAppName, MB_OK | MB_ICONEXCLAMATION);
      return NULL;

    case IDM_ABOUT:  /* display about dialog box */
      lpfnGenericDlgProc = MakeProcInstance((FARPROC)AboutDlgProc, hInst);
      DialogBox(hInst, "ABOUT", hWnd, lpfnGenericDlgProc);
      FreeProcInstance(lpfnGenericDlgProc); /* Release memory */
      return NULL;

    case IDM_HANDICAP:
      lpfnGenericDlgProc = MakeProcInstance((FARPROC)HandicapDlgProc, hInst);
      DialogBox(hInst, "HANDICAP", hWnd, lpfnGenericDlgProc);
      FreeProcInstance(lpfnGenericDlgProc); /* Release memory */
      return NULL;

    case IDM_P_SEARCH:
      if (!get_player(hWnd, &players, p_index, r_index))
	return NULL;
      /* fall through */

    case IDM_C_SEARCH:
      if (!get_course(hWnd, &courses, c_index))
	return NULL;
	wWhich = wParam;
	lpfnGenericDlgProc = MakeProcInstance((FARPROC)SearchDlgProc, hInst);
	DialogBox(hInst, "SEARCH", hWnd, lpfnGenericDlgProc);
	FreeProcInstance(lpfnGenericDlgProc); /* Release memory */
	return NULL;

    case IDM_GRAPH:
      if (!get_player(hWnd, &players, p_index, r_index))
	return NULL;
      if (!get_course(hWnd, &courses, c_index))
	return NULL;
      if (IDYES == MessageBox(hWnd, "You must have Microsoft Excel\nto use this option.  Continue?",
			      szAppName, MB_YESNO | MB_ICONQUESTION))
	GraphData(&(players).player[p_index].p_scores, &courses);
      return NULL;

    case IDM_NEW_P: /* save current file and clear viewport */
      if (bNeedSaveP && IDCANCEL == AskAboutSave(hWnd, szFileTitleP, PLAYER_MARKER))
	return FALSE;
      szFileTitleP[NULL] = '\0';
      bNeedSaveP = FALSE;
      p_index = r_index = r_last = 0;
      players.player_numb = -1;
      clear_player(hWnd, hWndRList);
      DoCaption(hWnd, szFileTitleP, szFileTitleC);
      return NULL;

    case IDM_NEW_C:
      if (bNeedSaveC && IDCANCEL == AskAboutSave(hWnd, szFileTitleC, COURSE_MARKER))
	return FALSE;
      szFileTitleC[NULL] = '\0';
      bNeedSaveC = FALSE;
      c_index = 0;
      courses.course_numb = -1;
      clear_course(hWnd);
      DoCaption(hWnd, szFileTitleP, szFileTitleC);
      return NULL;

    case IDM_OPEN_P:  /* open an existing file */
      of_player.lpstrTitle = "Open Player";
      of_player.Flags      = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;

      if (!GetOpenFileName(&of_player))
	return FALSE;

      if (ReadFile(hWnd, szFileP, szFileTitleP, szFileTitleC, bNeedSaveP,
		   &players, &courses, PLAYER_MARKER))
      {
	bNeedSaveP = FALSE;
	p_index = 0;
	show_player(hWnd, hWndRList, &players, &p_index, &r_index, &r_last);
	DoCaption(hWnd, szFileTitleP, szFileTitleC);
      } /* if */
      return NULL;

    case IDM_OPEN_C:  /* open an existing file */
      of_course.lpstrTitle = "Open Course";
      of_course.Flags      = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;

      if (!GetOpenFileName(&of_course))
	return FALSE;

      if (ReadFile(hWnd, szFileC, szFileTitleP, szFileTitleC, bNeedSaveC,
		   &players, &courses, COURSE_MARKER))
      {
	bNeedSaveC = FALSE;
	c_index = 0;
	show_course(hWnd, &courses, &c_index);
	DoCaption(hWnd, szFileTitleP, szFileTitleC);
      } /* if */
      return NULL;

    case IDM_SAVE_P:  /* write current file */
      if (szFileTitleP[NULL])
      {
	if (!get_player(hWnd, &players, p_index, r_index))
	  return NULL;

	if (WriteFile(hWnd, szFileP, &players, &courses, PLAYER_MARKER,
		      p_index, r_index, c_index))
	{
	  bNeedSaveP = FALSE;
	  return 1;
	} /* if */
	return NULL;
      } /* if fall through */

    case IDM_SAVEAS_P:  /* change file name */
      if (!get_player(hWnd, &players, p_index, r_index))
	return NULL;

      of_player.lpstrTitle = "Save Player";
      of_player.Flags      = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;

      if (!GetSaveFileName(&of_player))
	return FALSE;

      if (WriteFile(hWnd, szFileP, &players, &courses, PLAYER_MARKER,
		    p_index, r_index, c_index))
      {
	bNeedSaveP = FALSE;
	DoCaption(hWnd, szFileTitleP, szFileTitleC);
      }
      return NULL;

    case IDM_SAVE_C:  /* write current file */
      if (szFileTitleC[NULL])
      {
	if (!get_course(hWnd, &courses, c_index))
	  return NULL;

	if (WriteFile(hWnd, szFileC, &players, &courses, COURSE_MARKER,
		      p_index, r_index, c_index))
	{
	  bNeedSaveC = FALSE;
	  return 1;
	} /* if */
	return NULL;
      } /* if fall through */

    case IDM_SAVEAS_C:  /* change file name */
      if (!get_course(hWnd, &courses, c_index))
	return NULL;

      of_course.lpstrTitle = "Save Course";
      of_course.Flags      = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;

      if (!GetSaveFileName(&of_course))
	return FALSE;

      if (WriteFile(hWnd, szFileC, &players, &courses, COURSE_MARKER,
		    p_index, r_index, c_index))
      {
	bNeedSaveC = FALSE;
	DoCaption(hWnd, szFileTitleP, szFileTitleC);
      }
      return NULL;

    case IDM_PRINT_P:  /* Print current file */
      if (!get_player(hWnd, &players, p_index, r_index))
	return NULL;
      PrintFile(hInst, hWnd, szFileTitleP[NULL] ? szFileTitleP : szUntitled);
      return NULL;

    case IDM_PRINT_C:  /* Print current file */
      if (!get_course(hWnd, &courses, c_index))
	return NULL;
      PrintFile(hInst, hWnd, szFileTitleC[NULL] ? szFileTitleC : szUntitled);
      return NULL;

    case IDM_EXIT:  /* Send a close message, and exit the program */
      SendMessage(hWnd, WM_CLOSE, NULL, 0L);
      return NULL;

    case IDM_UNDO:  /* Check for undo status */
      SendMessage(GetFocus(), WM_UNDO, 0, 0L);
      return 0;

    case IDM_CUT:  /* Send cut message */
      SendMessage(GetFocus(), WM_CUT, 0, 0L);
      return 0;

    case IDM_COPY: /* Send copy message */
      SendMessage(GetFocus(), WM_COPY, 0, 0L);
      return 0;

    case IDM_PASTE:  /* Send paste message */
      SendMessage(GetFocus(), WM_PASTE, 0, 0L);
      return 0;

    case IDM_CLEAR:  /* Send clear message */
      SendMessage(GetFocus(), WM_CLEAR, 0, 0L);
      return 0;

    case IDM_P_ADD:
      if (AddPlayer(hWnd, &players, &p_index, &r_index))
	bNeedSaveP = TRUE;
      return NULL;

    case IDM_R_ADD:
      if (AddRound(hWnd, &players, &p_index, &r_index, &r_last))
	bNeedSaveP = TRUE;
      return NULL;

    case IDM_C_ADD:
      if (AddCourse(hWnd, &courses, &c_index))
	bNeedSaveC = TRUE;
      return NULL;

    case IDM_P_SORT:
      if (!get_player(hWnd, &players, p_index, r_index))
	return NULL;
      qsort(players.player, players.player_numb+1, sizeof(players.player[0]),
	    compare_player);
      bNeedSaveP = TRUE;
      p_index = r_index = 0;
      show_player(hWnd, hWndRList, &players, &p_index, &r_index, &r_last);
      return NULL;

    case IDM_R_SORT:
      if (!get_player(hWnd, &players, p_index, r_index))
	return NULL;
      qsort(players.player[p_index].p_scores.history,
	    players.player[p_index].p_scores.round_numb+1,
	    sizeof(players.player[p_index].p_scores.history[0]),
	    compare_round);
      bNeedSaveP = TRUE;
      r_index = 0;
      show_player(hWnd, hWndRList, &players, &p_index, &r_index, &r_last);
      return NULL;

    case IDM_C_SORT:
      if (!get_course(hWnd, &courses, c_index))
	return NULL;
      qsort(courses.course, courses.course_numb, sizeof(courses.course[0]),
	    compare_course);
      bNeedSaveC = TRUE;
      c_index = 0;
      show_course(hWnd, &courses, &c_index);
      return NULL;

    case IDM_P_DELETE:
      if (players.player_numb > -1)
      {
	GetDlgItemText(hWnd, IDD_PLAST, (players).player[p_index].p_last, LAST_NAME_LENGTH+1);
	GetDlgItemText(hWnd, IDD_PFIRST, (players).player[p_index].p_first, FIRST_NAME_LENGTH+1);
	lstrcpy(szDelete, "Delete : ");
	lstrcat(szDelete, (LPSTR)players.player[p_index].p_first);
	lstrcat(szDelete, " ");
	lstrcat(szDelete, (LPSTR)players.player[p_index].p_last);
	if (IDYES == MessageBox(hWnd, szDelete, "Delete Player", MB_YESNO | MB_ICONQUESTION))
	{
	  bNeedSaveP = TRUE;
	  delete_player(hWnd, hWndRList, &players, &p_index, &r_index, &r_last);
	}
      } /* if */
      return NULL;

    case IDM_R_DELETE:
      if (players.player[p_index].p_scores.round_numb > -1)
      {
	convert_date(&players.player[p_index].p_scores.history[r_index].date,
		     szDate);
	lstrcpy(szDelete, (LPSTR)"Delete : ");
	lstrcat(szDelete, (LPSTR)players.player[p_index].p_scores.history[r_index].course_name);
	lstrcat(szDelete, "  ");
	lstrcat(szDelete, szDate);
	if (IDYES == MessageBox(hWnd, szDelete, "Delete Round", MB_YESNO | MB_ICONQUESTION))
	{
	  delete_round(hWnd, hWndRList, &players, &p_index, &r_index, &r_last);
	  bNeedSaveP = TRUE;
	} /* if */
      } /* if */
      return NULL;

    case IDM_C_DELETE:
      if (courses.course_numb > -1)
      {
	GetDlgItemText(hWnd, IDD_CNAME, (courses).course[c_index].c_name, COURSE_NAME_LENGTH+1);
	lstrcpy(szc_name, (courses).course[c_index].c_name);
	lpfnGenericDlgProc = MakeProcInstance((FARPROC)DeleteCourseDlgProc, hInst);
	DialogBox(hInst, "DELETEC", hWnd, lpfnGenericDlgProc);
	FreeProcInstance(DeleteCourseDlgProc); /* Release memory */
	if (bYesNo == IDYES)
	{
	  bNeedSaveC = TRUE;
	  delete_course(&courses, &c_index);
	  if (bDeleteRounds &&  delete_rounds(&players, szc_name))
	  {
	    bNeedSaveP = TRUE;
	    show_rounds(hWnd, hWndRList, &(players).player[p_index].p_scores, &r_index, &r_last, TRUE);
	  } /* if */
	  show_course(hWnd, &courses, &c_index);
	} /* if */
      } /* if */
      return NULL;

    case IDD_RLIST:
      switch(HIWORD(lParam))
      {
	case LBN_DBLCLK:
	  if ((players.player_numb == -1) ||
	      (players.player[p_index].p_scores.round_numb == -1))
	    return NULL;
	  lstrcpy(szNDTitle, "Change Course / Name Date");
	  convert_date(&players.player[p_index].p_scores.history[r_index].date, szDate);
	  get_date_elements(szDate, szmm, szdd, szyy);
	  lstrcpy(szc_name, (LPSTR)players.player[p_index].p_scores.history[r_index].course_name);
	  lpfnGenericDlgProc = MakeProcInstance((FARPROC)NameDateDlgProc, hInst);
	  DialogBox(hInst, "NAMEDATE", hWnd, lpfnGenericDlgProc);
	  FreeProcInstance(NameDateDlgProc); /* Release memory */
	  if (bNameDate == IDOK)
	  {
	    bNeedSaveP = TRUE;
	    players.player[p_index].p_scores.history[r_index].date =
	      string_to_date(szmm, szdd, szyy);
	    lstrcpy((LPSTR)players.player[p_index].p_scores.history[r_index].course_name,
		    szc_name);
	    show_rounds(hWnd, hWndRList, &(players).player[p_index].p_scores, &r_index, &r_last, FALSE);
	  } /* if */
	  return NULL;

	case LBN_SELCHANGE:
	  if ((int)SendMessage(hWndRList, LB_GETCURSEL, 0, 0L) == r_index)
	    break;
	  if (!get_round(hWnd, &players, p_index, r_index))
	  {
	    SendMessage(hWndRList, LB_SETCURSEL, r_index, 0L);
	    return NULL;
	  }
	  r_index = (int)SendMessage(hWndRList, LB_GETCURSEL, 0, 0L);
	  if (r_index != LB_ERR && r_index != r_last)
	  {
	    show_round(hWnd, players.player[p_index].p_scores.history[r_index].score);
	    r_last = r_index;
	  } /* if */
	  return NULL;
      }  /* switch (HIWORD(lParam)) */
      return NULL;

    default:
      return FALSE;
  }  /* switch (wParam) */
} /* CommandHandler */