• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays in unfinished Noughts and Crosses game  RSS feed

 
Hannah Ahmed
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was given a piece of unfinished code to try and complete and I have failed miserably.

I have no idea how to include arrays to check if a move is valid or if the game has been won or drawn, I don't know how to initialize a board either.
For the checking if a move is valid I know I need to include a 2d array only allowing moves that are <5, and to check if a game has been drawn I can only allow the game to go on for 9 moves, but I have no idea how to implement this into my code.

I would greatly appreciate any help with this.

 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you really got to start from a particular piece of code?
I suggest you start by writing down the rules of noughts and crosses. Then consider what sort of pseudo‑code would implement those rules.
 
Hannah Ahmed
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have figured out how to make sure the move is valid by adding an if and else statement. The code now looks like this



I am now working on the public static boolean checkXOrOHasWon(char[][] board)

I know I'll need a 2d array for this, but I'm not sure how about how I should go about doing this. Will I have to tell the program every horizontal, vertical and diagonal win, or can it be done easier.

I have also found that I can over-write a player's move if I enter the same coordinates, but I will try fixing this after I get the checkXOrOHasWon working.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said before, were you really given that code? It is pretty dreadful and not object‑oriented.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!