User Tools

Site Tools


a_2048_gui

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
a_2048_gui [2026/02/06 14:06] – [Additional Methods For the Game2048 class] frchrisa_2048_gui [2026/02/12 16:36] (current) frchris
Line 7: Line 7:
   - ''public int getScore()'' returns the score.     - ''public int getScore()'' returns the score.  
   - ''public int[][] getBoard()'' returns the gameBoard.   - ''public int[][] getBoard()'' returns the gameBoard.
-  - ''public int max()'' returns the maximum element of the gameBoard.    +  - ''public int getMax()'' returns the maximum element of the gameBoard.     
 + 
 +Also add ''private int max;'' to store the highest power of 2 acheived. 
  
 ===== Implementing Scoring ===== ===== Implementing Scoring =====
Line 33: Line 36:
  *    *  
   @author Chris Thiel, OFMCap   @author Chris Thiel, OFMCap
-  @version July 15, 2025+  @version Feb 10 2026
  *    *  
- *  Make sure to write a getScore() method in your Game2048 class + *  Add the attrute: 
-  as well as a getGameBoard() method.  Alternatively, +    private int max; // highest power of 2 acheived 
-  you can write your own draw(Graphics gmethod, and call it +   
-  from this class's paintComponent method.+  Add these methods to your Game2048 class: 
 +    public int getScore()  
 +    public int[][] getGameBoard() 
 +    public int getMax()  
 +    public boolean gameOver() 
  */  */
  
Line 53: Line 60:
  String message;  String message;
  private Game2048 myGame;  private Game2048 myGame;
- + private String version = "Activity 2";
  
  public GUI2048()  public GUI2048()
Line 78: Line 85:
  }  }
  public void paintComponent(Graphics g){  public void paintComponent(Graphics g){
- super.paintComponent(g); +          super.paintComponent(g); 
- g.setColor(Color.WHITE); +          g.setColor(Color.WHITE); 
- g.fillRect(0, 0, getWidth(),getHeight()); +          g.fillRect(0, 0, getWidth(),getHeight()); 
- g.setColor(Color.BLUE); +          g.setColor(Color.BLUE); 
-  +  
- g.setFont(new Font("Arial", Font.PLAIN, 48)); +          g.setFont(new Font("Arial", Font.PLAIN, 48)); 
- g.drawString("2048 Game", WIDTH/3, 40); +          g.drawString("2048 Game", WIDTH/3, 40); 
- g.setFont(titleFont); +          g.setFont(titleFont); 
- g.setColor(Color.BLACK); +          g.setColor(Color.BLACK); 
- g.drawString("Score: " + myGame.getScore(), 4*WIDTH/5, 40); +          g.drawString("Score: " + myGame.getScore(), 4*WIDTH/5, 30); 
- g.drawString( message , 20, WIDTH - 50); +          g.drawString("Max Rank: " + myGame.getMax(), 4*WIDTH/5, 50); 
- +          g.drawString( message , 20, WIDTH - 50); 
- g.setFont(regularFont); +  
- g.drawString("Version 1.0", 20, 50); +          g.setFont(regularFont); 
- g.setFont(new Font("Arial", Font.PLAIN, 48)); +          g.drawString(version, 20, 50); 
- g.setColor(Color.red); +          g.setFont(new Font("Arial", Font.PLAIN, 48)); 
- drawBoard(g); +          g.setColor(Color.red); 
- +          drawBoard(g); 
- +  
- }+    }
   
  public void drawBoard(Graphics g) {  public void drawBoard(Graphics g) {
Line 112: Line 119:
  }  }
  g.setColor(Color.BLACK);  g.setColor(Color.BLACK);
 +                int thickness = 3; // of the grid lines
  for(int r = 0; r <= boardSize; r++) {  for(int r = 0; r <= boardSize; r++) {
- g.drawLine(left, top + size*r, left + boardSize*size, top +size*r);+     for(int i = 0; i < thickness ; i++) 
 +                      g.drawLine(left, top + size*r+i, left + boardSize*size, top +size*r+i);
  }  }
  
  for(int c = 0; c <= boardSize; c++){  for(int c = 0; c <= boardSize; c++){
- g.drawLine(left+c*size, top ,left+c*size, top + boardSize* size);+                    for(int i = 0; i < thickness ; i++) 
 + g.drawLine(left+c*size+i, top ,left+c*size+i, top + boardSize* size);
  }  }
  }  }
Line 166: Line 176:
  myGame.add2ToBoard();  myGame.add2ToBoard();
  if (myGame.gameOver())  if (myGame.gameOver())
- message = "Game Over - You reached "+ myGame.max();+ message = "Game Over - You reached "+ myGame.getMax();
  repaint();  repaint();
  }  }
a_2048_gui.1770404788.txt.gz · Last modified: by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki