0

May You please suggest me how to implement computer's move inside game without having while loop in main method which will eventually burden the thread? As It is easy when I have game between 2 human players, because I can manage whose move it is by managing actionPerformed() method. So I dont need any loop.

I already have logic for computer's move inside computerPlays() method.

Probably it is bit easier if one player is human, one computer..where I can do something like: increase int n after each human move. and then in main()

if(n%2!=0)
{
computerPlays();
}

but I still need loop to have complete game flow.

What if both players are computer?

If You are willing to help me, please try using simpler words as I am pretty new to Java. Thanks.

edit: whats wrong with question ?

mike_cus
  • 67
  • 1
  • 10
  • Maybe some kind of state model, which when updated with a user move, calculates the computers move and updates the state....*"whats wrong with question ?"* - It's to broad. Why would you need the loop? Is there some kind of constant update process been executed (like painting)? How does the user state get updated? Is this a event driven environment instead? Where one action triggers a new state, so on and so forth? There's a lot of context missing which would need to be filled out before an appropriate answer could be made – MadProgrammer Oct 20 '17 at 23:13
  • 1
    An example is mentioned [here](https://stackoverflow.com/questions/46801830/implementing-pause-in-game-with-alternatly-switching-moves#comment80664454_46806596). – trashgod Oct 20 '17 at 23:14
  • @MadProgrammer probably javax.swing.TImer will help me. Well I didnt go that deep, because I think there is no need to, my question is (probably easy to someone more experienced than me) if players action is easy to follow because player is clicking buttons and making moves in game, how can i follow computer moves. if there is human vs computer I can do it but using while loop, I wanted to know how to do it without. and especially if both players are computer. because I just have method computerPlays() with implemented logic for 1 move. in main i need loop probably.i dont know so i asked :) – mike_cus Oct 20 '17 at 23:27
  • @MadProgrammer dont get me wrong I am open to criticism, I am here just to learn, and I really do my best to explain the problem, probably I dont have enough knowledge to percive all the gapes in my questions, that is why i asked whats wrong with a question, to make it better next time, and thanks for your answer, I really appreciate ! – mike_cus Oct 20 '17 at 23:33
  • But what you’ve negated to mention is what you’re ultimately trying to achieve - do you need a “main loop” or can you use a event driven process? Basically, do you need a real time system? – MadProgrammer Oct 20 '17 at 23:34
  • @MadProgrammer (hope I understood well) I would like to have main loop if such wouldnt block GUI, but preferably I would like to have real time system. At the moment I want to achieve to execute all computer moves till the end of the game with least burden to the GUI. Later I want to have computer vs computer games where I will be testing different intelligent system algorithms efficency. If my answer is not good enough, I am so under your level of knowledge, and I am sorry about that. – mike_cus Oct 20 '17 at 23:44
  • @MadProgrammer but where all started was how to have event driven system when computer is playing, and what are the events that occur during the game..lets say computer vs. computer, nobody is pressing any buttons or so..I could figure out with while loop in main() but it would burden the GUI in my opinion – mike_cus Oct 20 '17 at 23:47
  • 1
    Ah, okay, a real time system, so that would suggest that a `Timer` would suit your purposes then :) – MadProgrammer Oct 21 '17 at 01:15

2 Answers2

2

Use a javax.swing.Timer to pace serial calls to the model, stopping the timer when the game ends. In the game cited here, press A to toggle animation when using the mouse. Each time you release the mouse, the computer, via the timer's handler, will first move the player and then advance the enemies; it repeats this sequence until there is no valid move or the game is won.

/** Animate one move; stop when no more moves or game won. */
public void actionPerformed(ActionEvent e) {
    if (!game.move(mouseRow, mouseCol) || game.won()) timer.stop();
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

Your question seems to be vague, but i'll try to do my best, with general tips.

You should not use a loop to manage your game logic, this will make your GUI unresponsive, or totally freeze it.

Instead, you should use events to run the game. You could write a class which models your game logic, keeping an instance of this class in your "game panel". Then, you register all the listeners you need to determine when the player is trying to make his move. Whenever an action is fired you could:

  • Ask your game class if the player has to move.
  • If not, you can ignore that event, or you can send a warning/error message to the user.
  • Else, you set the current state of your game class, doing all you need to prepare it for the next move.
  • Refresh your game panel (if needed) to make all the changes visible.
  • If the computer has to move, you invoke the corresponding method.
  • Once again, you refresh your game panel, and so on ...

As you can see, this way of programming is event driven, when something happens you need to change your model, and then you update your view.

If the computer and the player play the game by alternately moving, it's easy to make your computer play after every player move, else, you need to check who is moving every single time.

Also, since the computer could play too fast to let the user understand what happenened, you should consider using a Timer (see the official tutorial) to add some delay to every move.

Hope this helps!

Ansharja
  • 1,237
  • 1
  • 14
  • 37
  • thanks for answer. probably my question wasnt good enough but in my opinion you repeated most of the stuff from my question hah. probably your answer should be my question :) luckily i already got the answer, but my question was so what would be event to follow if computer is playing and not human(who is pressing a button and creating events), and how to do it without any loop which would block GUI :) thanks anyway really – mike_cus Oct 20 '17 at 23:37