Welcome to MSDN Blogs Sign in | Join | Help

Intern Update: August 23rd

Nuo

 Nuo writes:

 

It’s been a long time since my last post. I was too busy working on another starter kit. Now everything is almost completed so I sit down and write this blog post.

I should have written this blog post yesterday, but our team had a golfing and picnic event so I didn’t had time to write blog. I enjoyed the event so much. It’s my first time golfing, and I already began loving it.

For the Tic Tac Toe starter kit, I’ve finished it for some time but I promised to talk about the mechanism here so I will do it now. Let me talk about the final part, how I did the user versus computer game logic.

An array of nine Boolean values is created as a global variable (BOOL bU2UGameData[]) to track (hold state) whether a specific window is already occupied. Two other Boolean arrays (x[] and o[]) are created as global variables to hold states of which grids are occupied by X and O’s respectively for the user-versus-computer game. A Boolean global variable named bU2UGame distinguishes whether the game is user-versus-user or user-versus-computer.

In DoRecognize(HWND, int), when a X or an O is recognized, the application checks for a winner by calling DoU2UWinnerCheck(hwnd, int, char*,  LPCWSTR). If no one wins, it checks whether it is a user-versus-user game by testing the value of bU2UGame. If it is not and user-versus-user game, it calls DoAI(char*) to perform a computer move.

DoU2UWinnerCheck(HWND, int, char*, LPCWSTR) does the following:

1.       Changes the specific value in bU2UGameData[] to TRUE.

2.       Adds the current letter to the global result set.

3.       Invalidates the client area so that WM_PAINT is triggered to display a bitmap showing the correct letter.

4.       Calls a Boolean function IsWinU2U(int) to determine whether that move wins the game.

If all bU2UGameData[] values are already set to TRUE and no one wins, DoU2UWinnerCheck(HWND, int, char* LPCWSTR) pops up a MessageBox to show the “Tie” message.

IsWinU2U(int) tests whether three bU2UGameData[] values are all true in the same line and whether they are all the same letter. Another global variable of char*[] is created (g_ResultSet[]) to store the recognition result of each window.

If this is a user-versus-computer game (bU2UGame = FALSE), then DoAI(char*) is called to make a computer move.

DoAI(char*) does the following:

·         Compares the recognized string (passed into the function as a parameter) with X and O, to see which letter the computer move should use.

·         Generates a random number between 1 and 10 for the skill level test. There is a global variable named g_ilevel which stores the skill level the user chooses (easy means g_ilevel = 0, medium means g_ilevel = 5, hard means g_ilevel = 10.) If the randomly generated number is greater than g_ilevel, the game performs a random move by calling RandomMove(char*); otherwise it make a smart move by calling ComAllWinMove(char*).

RandomMove( char*) generates another random number from 0 – 8, stores the variable, and tests if bU2UGameData[ran] is TRUE (meaning that window is occupied), then updates ran, until it gets a number whose bU2UGameData[ran] value  is FALSE. Then it calls DoMove(HWND, int, char*)  to make the move.

CompAllWinMove(char*) gets the letter the computer move should be using from its parameter, then it checks whether there are any two of the same letter already in a line (e.g., if(o[0]+o[1] == 2 && bU2UGameData[0] + bU2UGameData[1] = bU2UGameData[2] != 3)). It checks every possible case until it finds one, and then calls DoMove(HWND, int, char*). If no such case is found, it checks whether there are two opposite letters in a line and the third one is missing (e.g. if(x[0]+x[1] == 2 && bU2UGameData[0] + bU2UGameData[1] = bU2UGameData[2] != 3)).

It also checks every possible combination of moves until it finds one, and calls DoMove(HWND, int, char*) to make that move  in the missing window. If no such moves can be made, it checks whether the center window has been occupied and makes a move there. If the center window is occupied by the opposite letter, it then tries to make a move to any of the four corner windows (0, 2, 6, or 8). If none of these moves can be made, then it calls RandomMove(char*) for a random move.

DoMove(HWND, int, char*) calls DoU2UWinnerCheck(HWND, int, char*, LPCWSTR) to change all related Boolean variables as TRUE (”occupied”) and test for a win.

The user versus computer AI does the right thing. However, it’s certainly not the best AI from the game points of view. Concerning the main audience of this starter kit is ISV, OEM, and individual developers who interest in learning programming with Windows Mobile Ink, I’d rather think using this easy-to-understand AI is better than using a fantastic AI but no one understands.

This starter kit is going to be published soon. For more information, look at the published version and its documentation. I tried my best efforts to do it, and I hope you will like it.

Nuo

 

Published Friday, August 24, 2007 11:52 AM by johnkenn

Comments

Friday, August 24, 2007 5:44 PM by MSDN Blog Postings » Intern Update: August 23rd

# MSDN Blog Postings » Intern Update: August 23rd

Anonymous comments are disabled
 
Page view tracker