#Need help with Game UI. Using Library: java.awt and javax.swing .
1 messages ยท Page 1 of 1 (latest)
<@&987246487241105418> please have a look, thanks.
A panel with 3x3 Jbuttons, which will have a backup of 3x3 char array to map. And then, how do i take mouse clicks using ActionListener?
I uploaded your attachments as Gist. This makes them more accessible, for example to mobile users.
@dreamy stone FYI no DM help is allowed on this server
help takes place right here in the server ๐
Do try to put the game logic in a separate TicTacToe class
@olive shoal
Your question has been closed due to inactivity.
If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.
Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.
When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.
Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.
With enough info, someone knows the answer for sure ๐
This is my
TicTacToe.java:
public class TicTacToe{
static Character Player1 = 'X';
static Character Player2 = 'O';
static Character CurrentPlayer;
static int player1_score = 0;
static int Player2_score = 0;
static double roll;
TicTacToe()
{
roll = 1 + (Math.random() * (2 - 1 + 1));
roll = Math.floor(roll);
if (roll == 1) CurrentPlayer = Player2;
else CurrentPlayer = Player1;
}
public void haltGame(){
}
static protected void switchTurn(){
if(CurrentPlayer == Player1)
CurrentPlayer = Player2;
else
CurrentPlayer = Player1;
}
public int overallScore(){
return 0;
}
public boolean didPlayerwin(){
return false;
}
public boolean isMatchfound(){
return false;
}
}
Detected code, here are some useful tools:
[WARNING] The code couldn't end properly...
Problematic source code:
package Project_TicTacToe.TicTacToe;
public class TicTacToe{
static Character Player1 = 'X';
static Character Player2 = 'O';
static Character CurrentPlayer;
static int player1_score = 0;
static int Player2_score = 0;
static double roll;
TicTacToe()
{
roll = 1 + (Math.random() * (2 - 1 + 1));
roll = Math.floor(roll);
if (roll == 1) CurrentPlayer = Player2;
else CurrentPlayer = Player1;
}
public void haltGame(){
}
static protected void switchTurn(){
if(CurrentPlayer == Player1)
CurrentPlayer = Player2;
else
CurrentPlayer = Player1;
}
public int overallScore(){
return 0;
}
public boolean didPlayerwin(){
return false;
}
public boolean isMatchfound(){
return false;
}
}```
Cause:
The code doesn't compile:
illegal start of expression
## System out
[Nothing]
So here's my question:
How should i update overallscore and ismatchfound
(FYI ur abusing static for accessibility instead of what its actually meant for)
but then having a constructor makes little sense
ur mixing things in a way that it doesnt make sense
if u have one common instance u shouldnt use static. u should use static if u dont have instances
oh ok.
there is no reason to use static here at all
if u run into an issue without it, static isnt the solution
๐
ok..
im not sure what u mean regarding ur question bc i feel like im missing context, only seeing this part of the code
where is the code that controls these methods
the actual logic
so should i post other files also ?
overall i feel like ur approaching this in a suboptimal way
there should be some data class that holds the games state
and then the logic classes should have access to those
so they can answer their questions
so u should for example have a class that represents the game "board" state
i.e. the 3x3 fields
Window.java is only to create UI.
Events.java is for interaction.
and that class should be a data class only
I uploaded your attachments as Gist. This makes them more accessible, for example to mobile users.
meaning it should offer methods to get and set the state
and then everyone can have access to that data class and use it to answer stuff like "hasPlayerWon"
by checking the data
ok i understand btw
if u approach things in a structured way like that it becomes easy
yes. i do have my structure. i am creating 3x3 button array and 3x3 character array.
Creation of grid is in window.class
ActionListener is in Events.class which extends TicTacToe
In TicTacToe.class, there will be methods which will use score, haltgame, etc.
i am using character array so that i can later save a txt file containing winning text
but u dont have it in a data class which u give everyone access to
ur class layout isnt optimal for this and hence things are difficult
so im suggesting to improve that and suddenly everything will be easy
๐
the reason u cant code overallScore, didPlayerwin and isMatchfound in ur TicTacToe class is bc it doesnt have hands on that data in ur setup
how should it know if the player won if it cant check the board
i get it 
the virtual board should be in the TicTacToe class
And then your methods can't be static because they work on this
imagine you're writing tests to make sure your game works. when your tests run on github in a headless environment they won't be able to open a jpanel with buttons. One way around this is to make your tictactoe game work without a UI, test it, and then make a UI layer that uses the game. This is just one reason to separate the viewable part from the controlling part.
instead on github, i tried on terminal and it works.
Prints and updates board.
Empty fields are marked as _
At start symbols are random. 50 - 50 chance to get X / O symb.
But right now i am working on creating methods which provide data.
also prints Win statement with symbol. The only thing left is to check for stalemate
great. can you automate those things through the class API?
yes i am doing that now
Ok. My Character array game works at the back. Now i made two buttons: Re-match and reset board.
Re-match : Will only work when game ends. Player symbol shuffles.
Reset: Reset board anytime player wishes.
put them into JPanel. How do i attach its functions now ? i already made them
@olive shoal
Your question has been closed due to inactivity.
If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.
Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.
When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.
Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.
With enough info, someone knows the answer for sure ๐
.
i fixed above issues.
The next step:
I added a button: Rematch which will only be visible if its a draw or a winner is found. but it never works..
Here's my updated files:
I uploaded your attachments as Gist. This makes them more accessible, for example to mobile users.
Sample
@olive shoal
Your question has been closed due to inactivity.
If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.
Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.
When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.
Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.
With enough info, someone knows the answer for sure ๐
Update: My half screen is now fully functional.
Game Grid which registers X and O turn by Turn.
Announcers: win and draw
working rematch and reset board buttons.
rematch button only works IF a player has won / game draw, shuffling symbol
reset grid is used anytime, without Shuffling player symbol.
The next half, i want to put simple menu containing:
Score board, exit and change symbols