• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple game template

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey! I'm trying to learn java again and doing some exercises, I am struggling with understanding this task:
Write a code that handles tic-tac-toe game on a square board. I have a template like this:



my code:

rules: method setBoardSize should be called 1st, before calling setBoardSize, setO and setX should be ignored, calling toString() should return null, hasEmpty return false.

method setO - should set circle on board
method setX - should set X on board
method hasEmpty - return true untill board is full
method toString should generate actual board state and output smth like this (after calling System.out.println(reference to Test4)):
+-+-+-+
|O |X| |
+-+-+-+
| |X| |
+-+-+-+
| |O| |
+-+-+-+

program should communicate and ignore about wrong moves:
- 2 and more same moves (eg. Twice X in row)
- placing X/O outside the board
- placing X/O on a spot already taken
Starting position is (0,0) - upper left corner on board.
I do not need main method.
Thanks in advance for any help !
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks like the sort of code you get when you have weighed in and written code without thinking what you want to do. Why should you have a setX method at all? You need to go back and write down how one plays noughts and crosses. Write it down, in simple words. When you have written that description, you will be able to produce code.
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic