0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Think Java 12以降

Last updated at Posted at 2022-10-27

背景・理由

立派なプログラマーになるためにThink Javaを学びます。
chap12以降はゲーム作成なので別立てにします。
#リソース
https://books.trinket.io/thinkjava/

chap 12

語彙

encode
class variable
sequential search
binary search

# ex12_1
 public Card[] makeDeck(){
        Card[] cards = new Card[52];
        int index = 0;
        for (int suit = 0; suit <= 3; suit++) {
            for (int rank = 1; rank <= 13; rank++) {
                cards[index] = new Card(rank, suit);
                index++;
            }
        }
        return cards;
    }

# ex12_2

if (this.rank > that.rank){
         if (that.rank == 1) {
            return -1;
         } 
         else {
            return 1;
         }
      }

# ex12_3

   public static int[] suitHist(Card[] cards){
        int[] result = new int[4];
        for (Card card:cards){
            result[card.getSuit()] += 1;
        }
        return result;
    }
    public static boolean hasFlush(Card [] cards ){
        for (int count:suitHist(cards)){
            if (count >= 5){
                return true;
            }
        }
        return false;
    
    }

言い訳

・プログラム見る
・回答は他の人のを見ました。
・2Dは軽く目を通しました。

chap 13

語彙

pseudocode:
helper method:
top-down development:
selection sort:
merge sort:

# ex13_2
   public static int randomInt(int low, int high){
        return random.nextInt(Math.max(high, low) - Math.min(high, low) + 1) + low;
    }

    public void swapCards(int i, int j) {
        Card temp = cards[i];
        cards[i] = cards[j];
        cards[j] = temp;
    }

    public void shuffle(){
        for (int i=0;i<card.length;i++){
            swapCards(i, randomInt(i, card.length - 1));
        }
    }
# ex13_5
 public String toString() {
        StringBuilder result = new StringBuilder();
        for (int i= 0; i < this.cards.length; i++) {
            result.append(cards[i]);
            result.append('\n');
        }
        return result.toString();
    }

chap 14

語彙

collection:
subclass:
superclass:
wrapper method:
bottom-up development:
HAS-A:
IS-A:

# ex14_1
package ch14;//
//  EightsPlayer.java
//  Chapter14
//
//  Created by Apollo Zhu on 8/20/16.
//  Copyright © 2015-2016 WWITDC. All rights reserved.
//

public class EightsPlayer extends Player {

   private EightsCardCollection eightHand;

   public EightsPlayer(String name){
      super(name);
      eightHand = new EightsCardCollection(name);
   }
   
   /**
    * Gets the player's hand.
    */
   public EightsCardCollection getHand(){ return eightHand;}
   /**
    * Calculates the player's score (penalty points).
    */
   public int score() {
      int sum = 0;
      for(int i = 0; i < getHand().size(); i++){
         sum += getHand().getCardAt(i).score();
      }
      return sum;
   }

   /**
    * Displays the player's hand.
    */
   public void display() { getHand().display();}

   /**
    * Displays the player's name and score.
    */
   public void displayScore() { System.out.println(getName() + "has"  + score() + "points");}

   /**
    * Removes and returns a legal card from the player's hand.
    */
   public EightsCard play(Eights eights, EightsCard prev) {
      EightsCard card = searchForMatch(prev);
      if (card == null){
         card = drawForMatch(eights, prev);
      }
      return card;
   }

   public EightsCard searchForMatch(EightsCard prev) {
      for(int i=0; i < getHand().size(); i++){
         EightsCard card = getHand().getCardAt(i);
         if(card.matches(prev)){
            return getHand().removeAt(i);
         }
      }
      return null;
   }

   public EightsCard drawForMatch(Eights eights, EightsCard prev){
      while (true){
         EightsCard card = eights.draw();
         System.out.println(getName() + " draws " + card);
         if (card.matches(prev)){
            return card;
         }
         getHand().append(card);
      }
   }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?