/
Solitaire.java
115 lines (101 loc) · 3.66 KB
/
Solitaire.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*******************************************************************************
* Solitaire
*
* Copyright (C) 2016 by Martin P. Robillard
*
* See: https://github.com/prmr/Solitaire
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*******************************************************************************/
package ca.mcgill.cs.stg.solitaire.gui;
import ca.mcgill.cs.stg.solitaire.ai.GreedyPlayingStrategy;
import ca.mcgill.cs.stg.solitaire.model.FoundationPile;
import ca.mcgill.cs.stg.solitaire.model.GameModel;
import ca.mcgill.cs.stg.solitaire.model.TableauPile;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
/**
* Application class for Solitaire. The responsibility
* of this class is limited to assembling the major UI components
* and launching the application. All gesture handling logic is
* handled by its composed elements, which act as observers
* of the game model.
*/
public class Solitaire extends Application
{
private static final int WIDTH = 680;
private static final int HEIGHT = 500;
private static final int MARGIN_OUTER = 10;
private static final String TITLE = "Solitaire";
private static final String VERSION = "1.0";
/**
* Application head.
*/
public Solitaire() {}
/**
* Launches the application.
* @param pArgs This program takes no argument.
*/
public static void main(String[] pArgs)
{
launch(pArgs);
}
@Override
public void start(Stage pPrimaryStage)
{
pPrimaryStage.setTitle(TITLE + " " + VERSION);
GridPane root = new GridPane();
root.setStyle("-fx-background-color: green;");
root.setHgap(MARGIN_OUTER);
root.setVgap(MARGIN_OUTER);
root.setPadding(new Insets(MARGIN_OUTER));
final GameModel model = new GameModel(new GreedyPlayingStrategy());
DeckView deckView = new DeckView(model);
DiscardPileView discardPileView = new DiscardPileView(model);
root.add(deckView, 0, 0);
root.add(discardPileView, 1, 0);
for( FoundationPile index : FoundationPile.values() )
{
root.add(new SuitStack(model, index), 3+index.ordinal(), 0);
}
for( TableauPile index : TableauPile.values() )
{
root.add(new CardPileView(model, index), index.ordinal(), 1);
}
root.setOnKeyTyped(new EventHandler<KeyEvent>()
{
@Override
public void handle(final KeyEvent pEvent)
{
if( pEvent.getCharacter().equals("\r"))
{
model.tryToAutoPlay();
}
else if( pEvent.getCharacter().equals("\b"))
{
model.undoLast();
}
pEvent.consume();
}
});
pPrimaryStage.setResizable(false);
pPrimaryStage.setScene(new Scene(root, WIDTH, HEIGHT));
pPrimaryStage.show();
}
}