Sign in to follow this  
acidwillburnyou

Acid Burnz TicTacToe [AND OTHER C# QUESTIONS]

Recommended Posts

Decided to start up on this again but the thread was retired; so here it is: ====================ORIGINAL POST FROM HERE: Click Me!====================== Hey dudes and dudettes, as some of you know, I'm trying to learn c# with vs2005 express edition. My first self driven learning project is making a basic tic-tac-toe game and then improve it from there. Here is the interface i've chosen to work with: The user is to click on the button and depending on the turn, it will change to an X or an O on letting go of the button. First question: is this possible? If so, how would i do this? The "status" box will display whose turn it is, and maybe an upto date score count for both sides in the future. =========================================================================== [Edited by - acidwillburnyou on December 16, 2006 2:45:18 PM]

Share this post


Link to post
Share on other sites
ssrun    157
Generally speaking since I don't know C# (only C/C++) you can use a flag to indicate who's turn it is. Then when the user clicks in a valid square to place an X or O you flip the flag again. This process goes back and forth until the game ends. Just remember that you're only flipping the flag if the user makes a valid move (ie: clicks in an empty square).

Tic-tac-toe is actually a very interesting game. After you get it working with 2 players you may want to look into making an AI to play against. It's not too hard, you just need to look into GameTrees and the Minimax algorithm. The entire solution is a bunch of recursive calls.

Share this post


Link to post
Share on other sites
Problem:

byte PlayerAge;

Console.Write("Age (years): ");
PlayerAge = Console.Read();

Error:

Error 1 Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?) C:\Documents and Settings\Acid Burnz\Desktop\Programs\Mechnet Text RPG\Mechnet Text RPG\Program.cs 22 25 Mechnet Text RPG

Need:
..to know how I can have the user input their age as a number and not have to cast it from a string to an int. Or is that the only way?

Thanks, Acid

Share this post


Link to post
Share on other sites
TheUnbeliever    963
Console.Read() is not what you're looking for. It blocks, reading input one character a time until a newline. It returns the character code (hence the int) of the next character in the STDIN stream.

I suspect you're looking for something more like:

int PlayerAge;
try
PlayerAge = Int.Parse(Console.ReadLine());
catch(FormatException)
/* User entered something other than the just the string representation of
* an integer */
catch(OverflowException)
// User entered a number beyond the bounds of an int


Obviously the two catch statements are being over-cautious to an extent. You can do without them if you wish.

Share this post


Link to post
Share on other sites
blanky    364
Since there's only two players, just use a variable like bool firstPlayerTurn or something and switch it accordingly each turn (Set it to true when it's the first player's turn, and false when it isn't), based on that set the button's .Text property to an "X" or an "O". I suggest using Labels and stuff, it'd look neater, and you'd do it in the same way, just add a click event (I think you can just double click on the label to do this), but it's up to you. Also, take a look at the .Net forum section on these boards, more specialized .Net programmers go there. Of course this place is also appropriate because it's for beginners, I'm just saying, if you'd like to ask/talk with other .Net developers, go there :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this