• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Arrays in unfinished Noughts and Crosses game  RSS feed

 
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.

 
Marshal
Posts: 64494
225
  • 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: 64494
225
  • 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.
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!