Skip to content

Got one game working #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h1>TypeScript Casino</h1>
<div id="display"></div>
<div id="input">
<input type="text" name="user_input" id="user_input">
<input type="submit" value="submit">
<input type="submit" value="submit" id="submit_button">
</div>
</section>

Expand Down
9 changes: 9 additions & 0 deletions js/GameEngine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Casino {
export abstract class GameEngine<GameTypePlayer extends PlayerInterface, GameType extends GameInterface<GameTypePlayer>>
implements GameEngineInterface<GameTypePlayer, GameType> {

abstract getGame(): GameType
abstract evaluateTurn(player: GameTypePlayer): void
abstract run(): void
}
}
8 changes: 8 additions & 0 deletions js/GameEngineInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Casino {
export interface GameEngineInterface<GameTypePlayer extends PlayerInterface, GameType extends GameInterface<GameTypePlayer>> {

getGame(): GameType;
evaluateTurn(player: GameTypePlayer): void;
run(): void;
}
}
10 changes: 10 additions & 0 deletions js/GameInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Casino {
export interface GameInterface<T extends PlayerInterface> {

getPlayers(): T[];
getPlayer(playerId: number): T;
addPlayer(player: T): void;
removePlayer(player: T): void;
contains(played: T): boolean;
}
}
32 changes: 32 additions & 0 deletions js/MainMenu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Casino {
export class MainMenu {
displayElement: HTMLElement = document.getElementById("display");
submitButton: HTMLElement = document.getElementById("submit_button");
private userProfile: Profile;

constructor(){
this.menuStart = this.menuStart.bind(this);
this.createProfile = this.createProfile.bind(this);
this.gamePicker = this.gamePicker.bind(this);
}

public menuStart() {
this.displayElement.innerHTML = "Please enter your name";
this.submitButton.addEventListener("click",(e: Event) => this.createProfile(), {once: true});
}

private createProfile(){
this.userProfile = new Profile(Input.getInput());
this.displayElement.innerHTML += "<br>Hello " + this.userProfile.getuserName() +"!";
this.displayElement.innerHTML += "<br>Please select a game. <br>1. Slots";
this.submitButton.addEventListener("click",(e: Event) => this.gamePicker(), {once: true});
}

private gamePicker(){
if(Input.getInput().toLowerCase() === 'slots'){
var slotsGame = new SlotsGame();
slotsGame.startGame();
}
}
}
}
20 changes: 20 additions & 0 deletions js/Player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Casino {
export class Player implements PlayerInterface {

private playerProfile: Profile;

constructorI(playerProfile: Profile) {
this.playerProfile = playerProfile;
}

getProfile(): Profile {
return this.playerProfile;
}
getName(): string {
return this.playerProfile.getuserName();
}
getId(): number {
return this.playerProfile.getUserId();
}
}
}
8 changes: 8 additions & 0 deletions js/PlayerInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Casino {
export interface PlayerInterface {

getProfile(): Profile
getName(): string
getId(): number
}
}
27 changes: 27 additions & 0 deletions js/Profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Casino {
export class Profile {

private userId: number;
private userName: string;
private balance: number;

constructor(userName: string) {
this.userId = Math.random() * 1000;
this.userName = userName;
this.balance = 500;
}

public getUserId(): number {
return this.userId;
}

public getuserName(): string {
return this.userName;
}

public getBalance(): number {
return this.balance;
}

}
}
78 changes: 78 additions & 0 deletions js/SlotsGame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
namespace Casino{
export class SlotsGame{

submitButton: HTMLElement = document.getElementById("submit_button");
displayElement: HTMLElement = document.getElementById("display");
private slotWheel1: string[] = ["&#x06DE","&#x2655","&#x26C4","&#x221E","&#x2126","&#x2042"];
private slotWheel2: string[] = ["&#x2126","&#x06DE","&#x2042","&#x2655","&#x26C4","&#x221E"];
private slotWheel3: string[] = ["&#x2655","&#x26C4","&#x221E","&#x2042","&#x2126","&#x06DE"];


public startGame(){
var slotMachine = this.createMultipleWheelOutput();
this.displaySlotMachine(slotMachine);
this.checkWinners(slotMachine);
this.displayElement.innerHTML += "<br>Type exit to quit or anything else to play again."
this.submitButton.addEventListener("click",(e: Event) => this.endGameChecker(), {once:true});
}

private endGameChecker(){
if(Input.getInput().toLowerCase() != 'exit'){
this.startGame();
}
}

private createMultipleWheelOutput(): string[][]{
var slotMachine: string[][] = [];
slotMachine[0] = this.createSingleWheelOutput(this.slotWheel1);
slotMachine[1] = this.createSingleWheelOutput(this.slotWheel2);
slotMachine[2] = this.createSingleWheelOutput(this.slotWheel3);
return slotMachine;
}

private createSingleWheelOutput(slotWheel: string[]): string[]{
var newArray: string[] = [];
var position: number = Math.floor(Math.random() * 6);
for(var i = 0; i < 3; i++){
if(position == 6){
position = 0;
}
newArray[i] = slotWheel[position];
position++;
}
return newArray;
}

private displaySlotMachine(slotMachine: string[][]){
this.displayElement.innerHTML += "<br>" + slotMachine[0][0] +" " + slotMachine[1][0] +" "+ slotMachine[2][0];
this.displayElement.innerHTML += "<br>" + slotMachine[0][1] +" " + slotMachine[1][1] +" "+ slotMachine[2][1];
this.displayElement.innerHTML += "<br>" + slotMachine[0][2] +" " + slotMachine[1][2] +" "+ slotMachine[2][2];
}

private checkWinners(slotMachine: string[][]){
this.checkRowWinners(slotMachine);
this.checkDiagonalWinners(slotMachine);
}

private checkRowWinners(slotMachine: string[][]){
if(slotMachine[0][0] === slotMachine[1][0] && slotMachine[1][0]=== slotMachine[2][0]){
this.displayElement.innerHTML += "<br>You have won on row 1!";
}
if(slotMachine[0][1] === slotMachine[1][1] && slotMachine[1][1]=== slotMachine[2][1]){
this.displayElement.innerHTML += "<br>You have won on row 2!";
}
if(slotMachine[0][2] === slotMachine[1][2] && slotMachine[1][2]=== slotMachine[2][2]){
this.displayElement.innerHTML += "<br>You have won on row 3!";
}
}

private checkDiagonalWinners(slotMachine: string[][]){
if(slotMachine[0][0] === slotMachine[1][1] && slotMachine[1][1]=== slotMachine[2][2]){
this.displayElement.innerHTML += "<br>You have won on diagonal down!";
}
if(slotMachine[0][2] === slotMachine[1][1] && slotMachine[1][1]=== slotMachine[2][0]){
this.displayElement.innerHTML += "<br>You have won on diagonal up!";
}
}
}
}
166 changes: 166 additions & 0 deletions js/app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading