943,746 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 663
  • C# RSS
Jun 9th, 2009
0

Just began c#, Comment Please ?

Expand Post »
I just started a few days ago. I am troubled with the input
in c#, even though I got it to work. Any comment would be
helpful. Its a Number Guessing game and a tic-tac-toe game.

C# Syntax (Toggle Plain Text)
  1. using System;
  2.  
  3. namespace GAME
  4. {
  5. #region NUMBER GUESSING GAME
  6. public class Number_Guess
  7. {
  8. public static void HiLow()
  9. {
  10. int guessNum = -1;
  11.  
  12. string gNum;
  13.  
  14. Random rand = new Random();
  15.  
  16. int minRan = rand.Next(10);
  17. int maxRan = rand.Next(50,150);
  18.  
  19. int goal = rand.Next(minRan, maxRan);
  20.  
  21. int numOfGuess = 0;
  22.  
  23. Console.WriteLine("Your object it to guess the random number from {0} to {1}\n\n",minRan,maxRan);
  24.  
  25. do
  26. {
  27.  
  28. Console.WriteLine("What will you guess : ");
  29.  
  30. gNum = Console.ReadLine();
  31.  
  32. guessNum = int.Parse(gNum);
  33.  
  34. if (guessNum == goal)
  35. {
  36. Console.WriteLine("You guessed correct!\n\n Press a key to continue ...");
  37. string x = Console.ReadLine();
  38.  
  39. break;
  40. }
  41.  
  42. else if (guessNum > goal)
  43. {
  44. Console.WriteLine("You guessed too high\n");
  45. ++numOfGuess;
  46.  
  47. }
  48.  
  49. else
  50. {
  51. Console.WriteLine("You guessed too low\n\n");
  52. ++numOfGuess;
  53. }
  54.  
  55. } while (guessNum != goal);
  56.  
  57.  
  58. }
  59.  
  60. }
  61. #endregion
  62. #region TIC-TAC-TOE
  63. public class BOARD_GAME
  64. {
  65. const int GAME_STRT = 0;
  66. const int GAME_OVER = 1;
  67. const int RESET = 2;
  68.  
  69.  
  70. public static void Tic_Tac_toe()
  71. {
  72.  
  73. Console.Title = "Tic-Tac-Toe";
  74.  
  75. //Create a The main GameBoard 3x3
  76. char[] GameBoard = new char[9]
  77. {
  78. '1','2','3',
  79. '4','5','6',
  80. '7','8','9'
  81. };
  82.  
  83. //Initialize
  84. char P1_ID = 'X';
  85. char Comp_ID = 'O';
  86. int GameState = GAME_STRT;
  87. int choice = -1;
  88.  
  89. Random rand = new Random();
  90.  
  91. Console.WriteLine("Would you like to be X or O : ");
  92.  
  93. P1_ID = (char)Console.Read();
  94.  
  95. //Dummy reader
  96. Console.ReadLine();
  97.  
  98. if (P1_ID == 'O' || P1_ID == 'o' || P1_ID == '0')
  99. { Comp_ID = 'X'; P1_ID = 'O'; }
  100.  
  101. else P1_ID = 'X';
  102.  
  103.  
  104. PrintBrd(GameBoard, -1, P1_ID);
  105. choice = GetInput(choice);
  106.  
  107.  
  108. Console.Clear();
  109.  
  110.  
  111. do
  112. {
  113.  
  114.  
  115. PrintBrd(GameBoard, choice, P1_ID);
  116.  
  117.  
  118. //Comp Turn
  119. choice = rand.Next(1, 9);
  120. //Check if Valid
  121. while (!(ValidMove(P1_ID, Comp_ID, GameBoard, choice)))
  122. choice = rand.Next(1, 9);
  123.  
  124. PrintBrd(GameBoard, choice, Comp_ID);
  125.  
  126. //Check winner, if any
  127. switch (CheckWinner(GameBoard, P1_ID, Comp_ID))
  128. {
  129.  
  130. case 'P': Console.WriteLine("\nCongrats, You beat the stupid computer\n"); GameState = GAME_OVER; goto case '-';
  131. case 'C': Console.WriteLine("\nHahaha, how the Hell did you loose???\n"); GameState = GAME_OVER; goto case '-';
  132. case 'D': Console.WriteLine("\nIts a Draw Game!\n\n"); GameState = GAME_OVER; goto case '-';
  133. case '-':
  134. char op = 'y';
  135. Console.WriteLine("\nPlaye Again? (y/n) : ");
  136. op = (char)Console.Read();
  137.  
  138. if (op == 'y' || op == 'Y')
  139. {
  140. GameState = RESET; break;
  141. }
  142. else GameState = GAME_OVER;
  143.  
  144. break;
  145.  
  146. default: break;
  147. }
  148.  
  149.  
  150. if (GameState == GAME_STRT)
  151. {
  152.  
  153. //Get user Input
  154. choice = GetInput(choice);
  155. //Check for valid move
  156. while (!(ValidMove(P1_ID, Comp_ID, GameBoard, choice)))
  157. choice = GetInput(choice);
  158.  
  159. PrintBrd(GameBoard, choice, P1_ID);
  160.  
  161. //Check winner, if any
  162. switch (CheckWinner(GameBoard, P1_ID, Comp_ID))
  163. {
  164.  
  165. case 'P': Console.WriteLine("\n\nCongrats, You beat the stupid computer\n"); GameState = GAME_OVER; goto case '-';
  166. case 'C': Console.WriteLine("\n\nHahaha, how the Hell did you loose???\n"); GameState = GAME_OVER; goto case '-';
  167. case 'D': Console.WriteLine("\n\nIts a Draw Game!\n\n"); GameState = GAME_OVER; goto case '-';
  168. case '-':
  169. char op = 'y';
  170. Console.WriteLine("\nPlaye Again? (y/n) : ");
  171. op = (char)Console.Read();
  172.  
  173. if (op == 'y' || op == 'Y')
  174. {
  175. GameState = RESET; break;
  176. }
  177. else GameState = GAME_OVER;
  178.  
  179. break;
  180.  
  181. default: break;
  182. }
  183.  
  184. }
  185.  
  186. if (GameState == RESET)
  187. {
  188. GameState = resetFunc(GameBoard, GameState);
  189. PrintBrd(GameBoard, -1, Comp_ID);
  190. Console.ReadLine();
  191. choice = GetInput(choice);
  192. }
  193.  
  194. } while (GameState != GAME_OVER);
  195.  
  196. }
  197.  
  198. #region void PrintBrdPrint(char[],int,char) //Prints Board
  199. static public void PrintBrd(char[] Brd, int ElemChoice, char Obj_ID)
  200. {
  201. Console.Clear();
  202.  
  203. //Check for validity
  204. if ((ElemChoice < 1 || ElemChoice > 9) && ElemChoice != -1)
  205. {
  206. Console.WriteLine("INVALID ROW CHOICE! ITS BEGIN IGNORED THIS TIME\n\n");
  207. return;
  208. }
  209.  
  210. else
  211. {
  212. if (ElemChoice != -1)
  213. Brd[ElemChoice - 1] = Obj_ID;
  214. }
  215.  
  216. //Print board
  217. for (int i = 0; i < 9; i += 3)
  218. {
  219. if (i != 0 && (i % 3 == 0))
  220. Console.WriteLine('\n');
  221.  
  222. Console.WriteLine("{0} | {1} | {2}", Brd[i], Brd[i + 1], Brd[i + 2]);
  223. }
  224. }
  225. #endregion
  226. #region ValidMove(char,char,char[],int) : Check For Valid Move
  227. public static bool ValidMove(char ID, char ID2, char[] GameBrd, int choice)
  228. {
  229. if (choice < 1 || choice > 9)
  230. {
  231. Console.WriteLine("\nInvalid Dimension\n");
  232. return false;
  233. }
  234.  
  235. if (GameBrd[choice - 1] == ID ||
  236. GameBrd[choice - 1] == ID2)
  237. {
  238. Console.WriteLine("\nInvalid Dimension\n");
  239. return false;
  240. }
  241.  
  242. return true;
  243. }
  244. #endregion
  245. #region GetInput(int) // Gets user Input
  246. static public int GetInput(int ch)
  247. {
  248. Console.WriteLine("\n\nWhere do you want to place your Move : ");
  249. ch = Console.Read();
  250. ch -= '0';
  251. //Dummy read
  252. Console.ReadLine();
  253. return ch;
  254. }
  255. #endregion
  256. #region CheckWinner(char [] GameBrd, char Obj_ID,char Obj2_ID) // checks for winner
  257. static public char CheckWinner(char[] Gbrd, char P_ID, char C_ID)
  258. {
  259.  
  260. char winner = '*'; // represent neither
  261.  
  262. for (int i = 0; i < 9; i++)
  263. {
  264. if (i % 3 == 0) //check horizontal win
  265. {
  266. if (Gbrd[i] == P_ID && Gbrd[i + 1] == P_ID && Gbrd[i + 2] == P_ID)
  267. { winner = 'P'; return winner; }
  268. else if (Gbrd[i] == C_ID && Gbrd[i + 1] == C_ID && Gbrd[i + 2] == C_ID)
  269. { winner = 'C'; return winner; }
  270.  
  271. }
  272.  
  273. if (i < 3) //check vertical win
  274. {
  275. if (Gbrd[i] == P_ID && Gbrd[i + 3] == P_ID && Gbrd[i + 6] == P_ID)
  276. { winner = 'P'; return winner; }
  277. else if (Gbrd[i] == C_ID && Gbrd[i + 3] == C_ID && Gbrd[i + 6] == C_ID)
  278. { winner = 'C'; return winner; }
  279. }
  280.  
  281. if (i == 0 ) //check diagnol win
  282. {
  283. if (Gbrd[i] == P_ID && Gbrd[i + 4] == P_ID && Gbrd[i + 8] == P_ID)
  284. { winner = 'P'; return winner; }
  285. else if (Gbrd[i] == C_ID && Gbrd[i + 4] == C_ID && Gbrd[i + 8] == C_ID)
  286. { winner = 'C'; return winner; }
  287. }
  288. else if (i == 2)
  289. {
  290. if (Gbrd[i] == P_ID && Gbrd[i + 2] == P_ID && Gbrd[i + 4] == P_ID)
  291. { winner = 'P'; return winner; }
  292. else if (Gbrd[i] == C_ID && Gbrd[i + 2] == C_ID && Gbrd[i + 4] == C_ID)
  293. { winner = 'C'; return winner; }
  294. }
  295.  
  296. }
  297.  
  298. //Check for draw game
  299. int cnt = 0;
  300.  
  301. for (int i = 0; i < 9; i++)
  302. {
  303. if (Gbrd[i] == P_ID || Gbrd[i] == C_ID)
  304. ++cnt;
  305. else break;
  306. }
  307.  
  308. if (cnt == 9)
  309. {
  310. winner = 'D';
  311. return winner;
  312. }
  313.  
  314. //no winner if reached
  315. return winner;
  316.  
  317.  
  318. }
  319. #endregion
  320. #region Reset Game
  321. static int resetFunc(char[] brd,int state)
  322. {
  323. for (char i = '1'; i <= '9'; i++)
  324. brd[i-'1'] = i;
  325.  
  326. return (state = GAME_STRT);
  327. }
  328. #endregion
  329. }
  330. #endregion
  331.  
  332.  
  333. }
  334.  
  335.  
  336. class mainProg
  337. {
  338. static void Main()
  339. {
  340. char p = 'y';
  341.  
  342. string whichGame = "1";
  343.  
  344. do
  345. {
  346. Console.Clear();
  347.  
  348. Console.WriteLine("Which Game do you want to play : \n");
  349. Console.WriteLine("1) Number Guessing game\n");
  350. Console.WriteLine("2) Tic-tac-toe Game\n");
  351.  
  352. whichGame = Console.ReadLine();
  353.  
  354. Console.Clear();
  355.  
  356. switch (int.Parse(whichGame))
  357. {
  358. case 1: GAME.Number_Guess.HiLow(); break;
  359. case 2: GAME.BOARD_GAME.Tic_Tac_toe(); break;
  360. }
  361.  
  362. //Dummy read
  363. Console.ReadLine();
  364.  
  365. } while (p == 'y');
  366. }
  367. }
Similar Threads
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Jun 9th, 2009
0

Re: Just began c#, Comment Please ?

I am not too sure what the actual question is, nor do I have time to look through all that code and decide what you may be having problems with. I'm not too sure what you are confused with regarding input, as there are a vast amount of different input types in c# and in the .Net framework. Please post more specific problems and more relevant code.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Jun 9th, 2009
0

Re: Just began c#, Comment Please ?

What is your "trouble" with the input?
gNum = Console.ReadLine();

guessNum = int.Parse(gNum);

can be written as
guessNum = int.Parse(Console.ReadLine());
-----------------------------------------------------------------
string x = Console.ReadLine();
can be written as
Console.ReadKey();

Is it this you want?

P.S. In the future use code tags, and don't post 1000 lines of code saying I'm in trouble.
Last edited by ddanbe; Jun 9th, 2009 at 2:56 pm.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Jun 9th, 2009
0

Re: Just began c#, Comment Please ?

I'm sorry, there is no "real " error. I am just query about c#
inputs, because I've been use to c++.

I see that they differ in return value, c++ returns both char or
numbers, while c# returns chars. I also realize that sometimes
the stream contains more junk, so I had to use a dummy readline().

Like this snippet :
C# Syntax (Toggle Plain Text)
  1. Console.WriteLine("Would you like to be X or O : ");
  2.  
  3. P1_ID = (char)Console.Read();
  4.  
  5. //Dummy reader
  6. Console.ReadLine();
Notice the Dummy reader. Is there a way to get around this.
I mean even in the simplest case, say where in a while loop
I ask the use to input a value, the loops reads the value and skips
a few iterations because of the junk in the stream.
So for example, if I do this :
C# Syntax (Toggle Plain Text)
  1. while(true)
  2. {
  3. Console.WriteLine("Enter something : ");
  4. int in = Console.Read();
  5. Console.WriteLine("you entered {0}",in);
  6. }

The stream returns more than one value?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Jun 9th, 2009
-1

Re: Just began c#, Comment Please ?

I'm not too sure about the C# console as I only use C# for windows apps and as code-behind in an ASP web apps. But perhaps it is returning a character array, which when cast to an int causes some issues. It could also be returning a string containing a null terminator at the end. I would read in all values as a string, then cast to an int using int.parse(string), a nifty little feature built into c#. I'm pretty sure that it throws an exception if the value isn't parseable, so you can just build a try/catch block around it for some easy input validation.

Unless this is for a class, I would recommend using C++ for console apps as it seems to have, from my experience, much more direct, unfluffy classes that use pointers rather than object references and therefore run much more quickly. C# is a more user-friendly environment that allows you to build very complex apps more quickly, but for the simpler apps like the ones you mentioned, it is definately an inferior language.
Last edited by skatamatic; Jun 9th, 2009 at 4:06 pm.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Jun 9th, 2009
0

Re: Just began c#, Comment Please ?

The Console.Read method always returns an int. C# is very picky about those things! Try while(1) for instance, will work in C++, won't work in C#! Because 1 is an integer NOT a boolean expression! That is why you have to typecast the Read method to a char else you get an int which is equal to the ASCII code of the char you typed. Also when you press the return key, windows appends a CR(=13) and a LF(=10) character to the inputstream. This is not junk, as you may think.
Last edited by ddanbe; Jun 9th, 2009 at 4:39 pm. Reason: typo
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Jun 9th, 2009
0

Re: Just began c#, Comment Please ?

Quote originally posted by skatamatic ...
I'm not too sure about the C# console
> But perhaps it is returning a character array
> It could also be returning a string containing a null terminator at the end

Ho boy, ho boy, please don't answer a question if you are not sure yourself!
Last edited by ddanbe; Jun 9th, 2009 at 4:50 pm.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Jun 9th, 2009
0

Re: Just began c#, Comment Please ?

quote : Unless this is for a class, I would recommend using C++

This is for practice, learning other language than c++.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Jun 9th, 2009
0

Re: Just began c#, Comment Please ?

Click to Expand / Collapse  Quote originally posted by ddanbe ...
> But perhaps it is returning a character array
> It could also be returning a string containing a null terminator at the end

Ho boy, ho boy, please don't answer a question if you are not sure yourself!
Well it turns out my prediction was pretty much right... And at least I let him know I'm no expert in that area, but at least I offered what I know to help.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: running bcp queryout through C# program
Next Thread in C# Forum Timeline: string





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC