Just began c#, Comment Please ?

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 1,151
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 145
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Just began c#, Comment Please ?

 
0
  #1
Jun 9th, 2009
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Just began c#, Comment Please ?

 
0
  #2
Jun 9th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,925
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Just began c#, Comment Please ?

 
0
  #3
Jun 9th, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,151
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 145
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Re: Just began c#, Comment Please ?

 
0
  #4
Jun 9th, 2009
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 :
  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 :
  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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Just began c#, Comment Please ?

 
-1
  #5
Jun 9th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,925
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Just began c#, Comment Please ?

 
0
  #6
Jun 9th, 2009
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
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,925
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Just began c#, Comment Please ?

 
0
  #7
Jun 9th, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,151
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 145
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Re: Just began c#, Comment Please ?

 
0
  #8
Jun 9th, 2009
quote : Unless this is for a class, I would recommend using C++

This is for practice, learning other language than c++.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Just began c#, Comment Please ?

 
0
  #9
Jun 9th, 2009
Originally Posted by ddanbe View Post
> 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC