#Need help with Game UI. Using Library: java.awt and javax.swing .

1 messages ยท Page 1 of 1 (latest)

olive shoal
#

I am creating a new project : tictactoe for logic improvement.
I have also uploaded a file which will only make UI.
i want help with, which methods i will need ? and what is Listener?

spring aspenBOT
#

<@&987246487241105418> please have a look, thanks.

olive shoal
#

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?

spring aspenBOT
# olive shoal

I uploaded your attachments as Gist. This makes them more accessible, for example to mobile users.

plush mortar
#

@dreamy stone FYI no DM help is allowed on this server

#

help takes place right here in the server ๐Ÿ‘Œ

wise laurel
#

Do try to put the game logic in a separate TicTacToe class

spring aspenBOT
#

@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 ๐Ÿ‘

olive shoal
#

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;
    }
    
}
spring aspenBOT
# olive shoal This is my TicTacToe.java: ```package Project_TicTacToe.TicTacToe; public class ...

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]
olive shoal
#

So here's my question:
How should i update overallscore and ismatchfound

plush mortar
#

(FYI ur abusing static for accessibility instead of what its actually meant for)

olive shoal
#

i want to keep static because it will only create 1 common instance.

#

maintain*

plush mortar
#

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

olive shoal
#

oh ok.

plush mortar
#

there is no reason to use static here at all

#

if u run into an issue without it, static isnt the solution

#

๐Ÿ‘

olive shoal
#

ok..

plush mortar
#

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

olive shoal
#

so should i post other files also ?

plush mortar
#

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

olive shoal
plush mortar
#

and that class should be a data class only

spring aspenBOT
plush mortar
#

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

olive shoal
#

ok i understand btw

plush mortar
#

if u approach things in a structured way like that it becomes easy

olive shoal
#

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

plush mortar
#

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

olive shoal
#

i get it 10outof10

wise laurel
#

the virtual board should be in the TicTacToe class

#

And then your methods can't be static because they work on this

trim timber
#

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.

olive shoal
#

also prints Win statement with symbol. The only thing left is to check for stalemate

trim timber
#

great. can you automate those things through the class API?

olive shoal
#

yes i am doing that now

olive shoal
#

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

spring aspenBOT
#

@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 ๐Ÿ‘

olive shoal
#

.

spring aspenBOT
olive shoal
spring aspenBOT
#

@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 ๐Ÿ‘

olive shoal
#

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