Skip to content

create a game look a like 2048, but you can customize the target of win and width of arena

License

Notifications You must be signed in to change notification settings

yudaph/game-2048

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

2048 Game

create a game look a like 2048, but with flexibility to customize the target value for win and the with of the arena

how to use

Clone from this repository

git clone https://github.com/yudaph/game-2048.git

Run the game with existing input and output which is command prompt

go run main.go

how to control the game :

  • a to move left
  • w to move top
  • s to move down
  • d to move right

development

this game is open for development such as change the input and/or output method

modify input

there is two ways to modify input, we can directly connect to game Input interface

type Input interface {
	MoveTo(action Move)
}

or using controller Input interface (recommended)

type Input interface {
	MoveDirection() (MoveDirection, error)
	SetGameSetting() (width, target int, err error)
}

modify output

and also there is two ways for modifying output, directly connect via game output interface

type Output interface {
	GetArena() [][]int
	IsWin() bool
	IsLost() bool
}

or using controller Output interface (recommended)

type Output interface {
	ShowArena([][]int)
	ShowWin([][]int)
	ShowLost([][]int)
}

don't forget to modify main.go if we decided to change via controller interface

func main() {
	games := game.NewGameStruct()
	
	// change in for new input process
	in := input.NewInputStruct()
	
	// change out for new output process
	out := output.NewOutputStruct()
	control := controller.NewController(games, in, out)
	control.StartGame()
}

modify game flow

we can also modify game flow via controller gameProgress function

func (c *controller) gameProgress() {
	// get desire move direction
	direction, err := c.Input.MoveDirection()
	if err != nil {
		fmt.Println(err)
		c.gameProgress()
		return
	}

	// move to desired direction,
	// translateMove is for translate controller.MoveDirection (get from c.MoveDirection) to game.move
	c.gameCore.MoveTo(translateMove(direction))

	// check is win state achieve
	if c.IsWin() {
		// display win
		c.Output.ShowWin(c.GetArena())
		return
	}

	// spawn new value in random position
	c.SpawnNewValue(2)

	// check is lost achieve
	if c.IsLost() {
		// display lost
		c.ShowLost(c.GetArena())
		return
	}

	// show current state of arena
	c.ShowArena(c.GetArena())

	// call gameProgress again to repeat the iteration
	c.gameProgress()
}

future development

  • -

About

create a game look a like 2048, but you can customize the target of win and width of arena

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages