Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.util.Random
- /**
- * Created by koko on 26/01/15.
- */
- trait Direction
- object Up extends Direction
- object Down extends Direction
- object Left extends Direction
- object Right extends Direction
- object Start15 extends Direction
- object Start2048 extends Direction
- abstract class Game {
- def start(): Unit
- def print(board: Iterable[Int]): Unit = {
- val listToString = board.map(elem => "%2d".format(elem)).map(elem => if (elem == " 0") " " else elem)
- listToString.sliding(4, 4).map(line => line.mkString(" | ", " | ", " | ")).foreach(line => println(line))
- }
- def selectionOfGame2(input: String): Unit = {
- while (true) {
- val string = Console.readLine("Вы прошли эту игру!!!\n Играть заново : введите 'replay'" +
- " \n Выбрать другую игру: введите 'otherGame' ")
- this.input(string) match {
- case Some(g) => g.start()
- case None => println("Ты ебанулся, такой игры нету!")
- }
- }
- }
- def input(input: String): Option[Game] = {
- input match {
- case "15" => Some(new Game15())
- case "2048" => Some(new Game2048())
- case x => None
- }
- }
- def parseInput(input: String): Option[Direction] = {
- input match {
- case "j" => Some(Down)
- case "k" => Some(Up)
- case "h" => Some(Left)
- case "l" => Some(Right)
- case x =>
- println("wrong input")
- None
- }
- }
- }
- class Game15 extends Game {
- def start(): Unit = {
- val board = shuffleBoard(winBoard, 1)
- play(board)
- }
- val n = 4
- type Board = List[Int]
- val winBoard = (1 to n * n - 1).toList :+ 0
- def play(b: Board): Unit = {
- print(b)
- parseInput(Console.readLine()) match {
- case Some(d) =>
- val moved = move(b, d)
- if (moved != winBoard) {
- play(moved)
- }
- else if (b == winBoard) {
- print(b)
- println("Win")
- }
- case None =>
- println("h - Left\nk - Up\nj - Down\nl - Right")
- play(b)
- }
- }
- def shuffleBoard(b: Board, moves: Int): Board = {
- if (moves == 0) {
- b
- }
- else {
- val d = Random.shuffle(List(Up, Down, Left, Right)).head
- val moved = move(b, d)
- shuffleBoard(moved, moves - 1)
- }
- }
- def move(b: Board, d: Direction): Board = {
- d match {
- case Up => moveU(b)
- case Down => moveD(b)
- case Left => moveL(b)
- case Right => moveR(b)
- }
- }
- def moveD(b: Board): Board = {
- val index = b.indexOf(0)
- val down = index + n
- if (index <= 11) {
- swap(b, index, down)
- }
- else b
- }
- def moveU(b: Board): Board = {
- val index = b.indexOf(0)
- val up = index - n
- if (index > n - 1) {
- swap(b, index, up)
- }
- else b
- }
- def moveL(b: Board): Board = {
- val index = b.indexOf(0)
- val left = index - 1
- if (index % n != 0) {
- swap(b, index, left)
- }
- else b
- }
- def moveR(b: Board): Board = {
- val index = b.indexOf(0)
- val right = index + 1
- if (index % n != 3) {
- swap(b, index, right)
- }
- else b
- }
- def swap(board: Board, idx1: Int, idx2: Int): Board = {
- val elem1 = board(idx1)
- val elem2 = board(idx2)
- board.updated(idx1, elem2).updated(idx2, elem1)
- }
- }
- class Game2048 extends Game {
- def start(): Unit = {
- play()
- }
- type Board = Array[Int]
- val length = 4
- val board: Board = Array.fill(length * length)(0)
- val winBoard: Board = Array.fill(length * length)(2)
- def play(): Unit = {
- print(board)
- parseInput(Console.readLine()) match {
- case Some(d) =>
- move(board, d)
- if (board != winBoard) {
- play()
- }
- else if (board == winBoard) {
- println("Win")
- }
- case None =>
- println("h - Left\nj - Up\nh - Left\nl - Right")
- play()
- }
- }
- def zeroTo2(b: Board): Unit = {
- val index = Random.nextInt(15)
- val indexResult = b(index)
- if (indexResult == 0) {
- b.update(index, 2)
- }
- else zeroTo2(b)
- }
- def move(b: Board, d: Direction): Unit = {
- d match {
- case Right => moveRight(b)
- case Left => moveLeft(b)
- case Up => moveUp(b)
- }
- }
- def moveUp(b: Board): Unit = {
- for (r <- 0 to 3) {
- for (i <- leftIndex(r) to leftIndex(r) by + 4) {
- val sum = b.slice(leftIndex(r), i + 5).sum > 0
- while (b(i) == 0 && sum) {
- for (j <- i to leftIndex(r) + 4 by - 4) {
- b.update(j, b(j - 4))
- b.update(j - 4, 0)
- }
- }
- if (i != leftIndex(r) && b(i) == b(i - 4)) {
- val result = b(i) + b(i - 4)
- b.update(i - 4, result)
- b.update(i , 0)
- }
- else if (i != 12&& i != 13&& i != 13&& i != 14&& i != 15 && b(i) == b(i + 4)) {
- val result = b(i) + b(i + 4)
- b.update(i, result)
- b.update(i + 4, 0)
- }
- }
- }
- zeroTo2(b)
- }
- def leftIndex(row: Int): Int = {
- row * length
- }
- def rightIndex(row: Int): Int ={
- (row * length) + (length - 1)
- }
- def move1(b: Board, from: Int => Int, to: Int => Int, by: Int, hod: Int): Unit = {
- val step = Math.abs(by)
- for (r <- 0 to 3) {
- for (i <- from(r) to to(r) by by ) {
- val sum = b.slice(i + step, from(r) + length).sum > 0
- while (b(i) == 0 && sum ) {
- for (j <- i to to(r) + hod by by) {
- b.update(j, b(j + step))
- b.update(j + step, 0)
- }
- }
- if (i != to(r) && b(i) == b(i + step) ) {
- val result = b(i) + b(i + 1)
- b.update(i, result)
- b.update(i + step, 0)
- }
- else if ( i != from(r) && b(i) == b(i - step)) {
- val result = b(i) + b(i - step)
- b.update(i - step , result)
- b.update(i, 0)
- }
- }
- }
- zeroTo2(b)
- }
- def moveRight(b: Board): Unit = move1(b, rightIndex, leftIndex, -1, 1)
- def moveLeft(b: Board): Unit = move1(b, leftIndex, rightIndex, 1, 2)
- }
- object Main {
- def main(args: Array[String]): Unit = {
- while (true) {
- val string = Console.readLine("Выберите игру :\n Пятнашки - введите '15' \n 2048 - введите '2048' ")
- this.selectionOfGame(string) match {
- case Some(g) => g.start()
- case None => println("Ты ебанулся, такой игры нету!")
- }
- }
- }
- def selectionOfGame(input: String): Option[Game] = {
- input match {
- case "15" => Some(new Game15())
- case "2048" => Some(new Game2048())
- case x => None
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement