daily pastebin goal
19%
SHARE
TWEET

game2048 py->cs

mvaganov Dec 5th, 2018 (edited) 24 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Game2048
  5. {
  6.     public class Coord {
  7.         public int row, col;
  8.         public Coord(int r, int c){
  9.             row = r; col = c;
  10.         }
  11.     }
  12.     public class Tile {
  13.         public int number;
  14.         private Coord location;
  15.         public ConsoleColor color;
  16.         public Tile(int number, Coord location, ConsoleColor color){
  17.             this.number = number;
  18.             this.location = location;
  19.             this.color = color;
  20.         }
  21.         public void print() { // without static, this function needs an instance
  22.             //# letters = "%5d" % board[row][col]
  23.             string letters = string.Format("{0,5}", number);
  24.             //# sys.stdout.write(letters)
  25.             //Console.ForegroundColor = color;
  26.             Console.Write(letters);
  27.             //Console.ForegroundColor = ConsoleColor.Black;
  28.         }
  29.     }
  30.  
  31.     //# import random
  32.     static Random random = new Random();
  33.  
  34.     /// <returns>a 2D array for the 2048 board, 4 rows and 4 columns per row.</returns>
  35.     //# def makeBoard():
  36.     public static Tile[][] makeBoard() {
  37.         //# """return a 2D array for the 2048 board, 4 rows and 4 columns per row."""
  38.         //# height, width = 4, 4
  39.         int height = 4, width = 4;
  40.         //# twoDlist = []
  41.         Tile[][] twoDlist = new Tile[height][];
  42.         //# for row in range(height):
  43.         for (int row = 0; row < height; row++) {
  44.             //# twoDlist.append([])
  45.             twoDlist[row] = new Tile[width];
  46.             //# for col in range(width):
  47.             for (int col = 0; col < width; col++){
  48.                 //# twoDlist[row].append(0)
  49.                 twoDlist [row] [col] = null;
  50.             }
  51.         }
  52.         //# return twoDlist
  53.         return twoDlist;
  54.     }
  55.     //# def printBoard(board):
  56.     /// <summary>print the 2048 board. Each value prints 4 characters wide.</summary>
  57.     /// <param name="board">Board.</param>
  58.     public static void printBoard(Tile[][] board){
  59.         //# """print the 2048 board. Each value prints 4 characters wide."""
  60.         //# import sys
  61.         //# for row in range( len(board) ):
  62.         for(int row = 0; row < board.Length; row++){
  63.             //# for col in range( len(board[row]) ):
  64.             for(int col=0; col < board[row].Length; col++){
  65.                 //# if board[row][col] == 0:
  66.                 if(board[row][col] == null){
  67.                     //# sys.stdout.write("    .")
  68.                     Console.Write("    .");
  69.                 }
  70.                 //# else:
  71.                 else {
  72.                     board[row][col].print();
  73.                 }
  74.             }
  75.             //# sys.stdout.write("\n\n")
  76.             Console.Write("\n\n");
  77.         }
  78.     }
  79.     //# def assignNumbers(board):
  80.     /// <summary>fill the 2048 board with non-zero values, for testing purposes</summary>
  81.     /// <param name="board">Board.</param>
  82.     public static void assignNumbers(Tile[][] board){
  83.         //# """fill the 2048 board with non-zero values, for testing purposes"""
  84.         //# value = 0
  85.         int value = 0;
  86.         //# for row in range( len(board) ):
  87.         for(int row = 0; row < board.Length; row++){
  88.             //# for col in range( len(board[row]) ):
  89.             for(int col = 0; col < board[row].Length; col++){
  90.                 //# board[row][col] = value
  91.                 board[row][col] = new Tile(value, new Coord(row, col), ConsoleColor.Black);
  92.                 //# value += 1
  93.                 value ++;
  94.             }
  95.         }
  96.     }
  97.     //# def flipHorizontal(board):
  98.     /// <summary>reverses every row in the 2048 board, so the last elements are first</summary>
  99.     /// <param name="board">Board.</param>
  100.     public static void flipHorizontal(Tile[][] board){
  101.         //# """reverses every row in the 2048 board, so the last elements are first"""
  102.         //# for row in range(len(board)):
  103.         for(int row = 0; row < board.Length; row++){
  104.             //# board[row].reverse()
  105.             System.Array.Reverse(board[row]);
  106.         }
  107.     }
  108.     //# def flipXY(board):
  109.     /// <summary>swaps columns and rows, flipping the board about the x/y diagonal, so upper-right becomes lower-left.</summary>
  110.     /// <param name="board">Board.</param>
  111.     public static void flipXY(Tile[][] board){
  112.         //# """swaps columns and rows, flipping the board about the x/y diagonal, so upper-right becomes lower-left."""
  113.         //# height = len(board)
  114.         int height = board.Length;
  115.         //# width = len(board[0])
  116.         int width = board[0].Length;
  117.         //# for row in range(height):
  118.         for(int row = 0; row < height; row++){
  119.             //# for col in range(row+1):
  120.             for(int col = 0; col < row+1; col++){
  121.                 //# temp = board[row][col]
  122.                 Tile temp = board[row][col];
  123.                 //# board[row][col] = board[col][row]
  124.                 board[row][col] = board[col][row];
  125.                 //# board[col][row] = temp
  126.                 board[col][row] = temp;
  127.             }
  128.         }
  129.     }
  130.     //# def findEmptySpots(board, thingToLookFor = 0):
  131.     /// <returns>a list of row/col pairs where board[row][col] == 0</returns>
  132.     /// <param name="board">Board.</param>
  133.     /// <param name="thingToLookFor">Thing to look for.</param>
  134.     public static List<Coord> findEmptySpots(Tile[][] board, Tile thingToLookFor = null){
  135.         //# """returns a list of row/col pairs where board[row][col] == 0"""
  136.         //# locations = []
  137.         List<Coord> locations = new List<Coord>();
  138.         //# for row in range(len(board)):
  139.         for(int row = 0; row < board.Length; row++){
  140.             //# for col in range(len(board[row])):
  141.             for(int col = 0; col < board[row].Length; col++){
  142.                 //# if board[row][col] == thingToLookFor:
  143.                 if(board[row][col] == thingToLookFor){
  144.                     //# locations.append([row, col]) # add a new list [row, col] to the list
  145.                     locations.Add(new Coord(row,col));
  146.                 }
  147.             }
  148.         }
  149.         //# return locations
  150.         return locations;
  151.     }
  152.     //# def findRandomEmptySpot(board):
  153.     /// <returns>a randomly chosen row/col pair where board[row][col] == 0 using random.choice(list)
  154.     /// if there is no empty spot in board, return None</returns>
  155.     /// <param name="board">Board.</param>
  156.     public static Coord findRandomEmptySpot(Tile[][] board){
  157.         //# """returns a randomly chosen row/col pair where board[row][col] == 0 using random.choice(list)
  158.         //# if there is no empty spot in board, return None"""
  159.         //# spots = findEmptySpots(board)
  160.         List<Coord> spots = findEmptySpots(board);
  161.         //# if len(spots) == 0:
  162.         if (spots.Count == 0){
  163.             //# return None
  164.             return null;
  165.         }
  166.         //# return random.choice(spots)
  167.         return spots[random.Next(spots.Count)];
  168.     }
  169.     //# def addToRandomSpot(board, value):
  170.     /// <summary>set a random empty spot (row/col) in board to value, and return True
  171.     /// if there are no empty spots in board, return False</summary>
  172.     /// <param name="board">Board.</param>
  173.     /// <param name="value">Value.</param>
  174.     public static bool addToRandomSpot(Tile[][] board, int value){
  175.         //# """set a random empty spot (row/col) in board to value, and return True
  176.         //# if there are no empty spots in board, return False"""
  177.         //# spot = findRandomEmptySpot(board)
  178.         Coord spot = findRandomEmptySpot(board);
  179.         //# if spot != None:
  180.         if (spot != null){
  181.             //# board[spot[0]][spot[1]] = value
  182.             ConsoleColor[] colors = {ConsoleColor.Red, ConsoleColor.Green, ConsoleColor.Blue};
  183.             ConsoleColor color = colors[random.Next() % colors.Length];
  184.             board[spot.row][spot.col] = new Tile(value, new Coord(spot.row, spot.col), color);
  185.             //# return True
  186.             return true;
  187.         }
  188.         //# return False
  189.         return false;
  190.     }
  191.     //# def initializeBoardWith2RandomValues(board):
  192.     public static void initializeBoardWith2RandomValues(Tile[][] board){
  193.         //# import random
  194.         int[] n = new int[]{2,4};
  195.         //# addToRandomSpot(board, random.choice([2,4]))
  196.         addToRandomSpot(board, n[random.Next(n.Length)]);
  197.         //# addToRandomSpot(board, random.choice([2,4]))
  198.         addToRandomSpot(board, n[random.Next(n.Length)]);
  199.     }
  200.  
  201.     //# def pullRowLeft(row):
  202.     /// <summary>if an element is 0 and the next one isn't 0:
  203.     ///     swap them, then return True
  204.     /// if no element in the row could be pulled left, return False</summary>
  205.     /// <returns>returns True if an element could be pulled left, as per rules of 2048.</returns>
  206.     /// <param name="row">Row.</param>
  207.     private static bool pullRowLeft(Tile[] row){
  208.         //# """returns True if an element could be pulled left, as per rules of 2048.
  209.         //# if an element is 0 and the next one isn't 0:
  210.         //# swap them, then return True
  211.         //# if no element in the row could be pulled left, return False"""
  212.         //# for i in range(len(row)-1):
  213.         for(int i = 0; i < row.Length-1; i++){
  214.             //# if row[i] == 0 and row[i+1] != 0:
  215.             if(row[i] == null && row[i+1] != null){
  216.                 //# temp = row[i]
  217.                 Tile temp = row[i];
  218.                 //# row[i] = row[i+1]
  219.                 row[i] = row[i+1];
  220.                 //# row[i+1] = temp
  221.                 row[i+1] = temp;
  222.                 //# return True
  223.                 return true;
  224.             }
  225.         }
  226.         //# return False
  227.         return false;
  228.     }
  229.     //# def mergeRowLeft(row, startIndex = 0):
  230.     /// <summary>if an element is not 0 and the next element has the same value:
  231.     ///     double the element, set next element to 0, return the element index
  232.     /// if no element in the row could be merged left, return -1</summary>
  233.     /// <returns>the index that was merged left, as per the rules of 2048</returns>
  234.     /// <param name="row">a list of elements</param>
  235.     /// <param name="startIndex">which element to start trying to merge (usually 0)</param>
  236.     public static int mergeRowLeft(Tile[] row, int startIndex = 0){
  237.         //# """returns the index that was merged left, as per the rules of 2048
  238.         //# row: a list of elements
  239.         //# startIndex: which element to start trying to merge (usually 0)
  240.         //# if an element is not 0 and the next element has the same value:
  241.         //# double the element, set next element to 0, return the element index
  242.         //# if no element in the row could be merged left, return -1"""
  243.         //# for i in range(startIndex, len(row)-1):
  244.         for(int i = startIndex; i < row.Length-1; i++){
  245.             //# if row[i] != 0 and row[i+1] == row[i]:
  246.             if(row[i] != null && row[i+1] != null && row[i+1].number == row[i].number) {
  247.                 //# row[i] = row[i] * 2
  248.                 ConsoleColor[] colors = {ConsoleColor.Red, ConsoleColor.Green, ConsoleColor.Blue};
  249.                 ConsoleColor color = colors[random.Next() % colors.Length]; // TODO function plz
  250.                 row[i] = new Tile(row[i].number * 2, new Coord(-1, i), color);
  251.                 //# row[i+1] = 0
  252.                 row[i+1] = null;
  253.                 //# return i #True
  254.                 return i;
  255.             }
  256.         }
  257.         //# return -1 #False
  258.         return -1;
  259.     }
  260.     //# def moveLeft(board):
  261.     /// <summary>pulls and merges elements in the given board to the left, as per 2048.
  262.     //# pull all elements as far left as possible
  263.     //# merges two elements if possible. if a merge happens, pull again.
  264.     //# the same element should never merge more than once during a move</summary>
  265.     /// <returns><c>true</c> if an element could be pulled or merged
  266.     /// <c>false</c> if no element could be pulled or merged</returns>
  267.     /// <param name="board">Board.</param>
  268.     public static bool moveLeft(Tile[][] board){
  269.         //# """pulls and merges elements in the given board to the left, as per 2048.
  270.         //# pull all elements as far left as possible
  271.         //# merges two elements if possible. if a merge happens, pull again.
  272.         //# the same element should never merge more than once during a move
  273.         //# return True if an element could be pulled or merged
  274.         //# return False if no element could be pulled or merged
  275.         //# """
  276.         //# elementCouldBeMergedOrPulled = False
  277.         bool elementCouldBeMergedOrPulled = false;
  278.         //# for r in range(0, len(board)):
  279.         for(int r = 0; r < board.Length; r++){
  280.             //# row = board[r]
  281.             Tile[] row = board[r];
  282.             //# mergeIndex = 0
  283.             int mergeIndex = 0;
  284.             //# aMergeCouldHappen = True
  285.             bool aMergeCouldHappen = true;
  286.             //# while aMergeCouldHappen:
  287.             while (aMergeCouldHappen){
  288.                 //# while pullRowLeft(row): elementCouldBeMergedOrPulled = True
  289.                 while(pullRowLeft(row)) { elementCouldBeMergedOrPulled = true; }
  290.                 //# mergeIndex = mergeRowLeft(row, mergeIndex)
  291.                 mergeIndex = mergeRowLeft(row, mergeIndex);
  292.                 //# if mergeIndex >= 0:
  293.                 if(mergeIndex >= 0) {
  294.                     //# elementCouldBeMergedOrPulled = True
  295.                     elementCouldBeMergedOrPulled = true;
  296.                 }
  297.                 //# if mergeIndex == -1:
  298.                 if(mergeIndex == -1){
  299.                     //# aMergeCouldHappen = False
  300.                     aMergeCouldHappen = false;
  301.                 }
  302.                 //# while pullRowLeft(row): pass
  303.                 while(pullRowLeft(row));
  304.                 //# mergeIndex += 1
  305.                 mergeIndex ++;
  306.             }
  307.         }
  308.         //# return elementCouldBeMergedOrPulled
  309.         return elementCouldBeMergedOrPulled;
  310.     }
  311.     //# def moveRight(board):
  312.     /// <summary>as moveLeft, except the board is flipped horizontally during the move</summary>
  313.     /// <returns><c>true</c>, if right was moved, <c>false</c> otherwise.</returns>
  314.     /// <param name="board">Board.</param>
  315.     public static bool moveRight(Tile[][] board){
  316.         //# """as moveLeft, except the board is flipped horizontally during the move"""
  317.         //# result = False
  318.         bool result = false;
  319.         //# flipHorizontal(board); result = moveLeft(board); flipHorizontal(board)
  320.         flipHorizontal(board); result = moveLeft(board); flipHorizontal(board);
  321.         //# return result
  322.         return result;
  323.     }
  324.     //# def moveUp(board):
  325.     /// <summary>as moveLeft, except board flipped about the x/y diagonal during the move</summary>
  326.     /// <returns><c>true</c>, if up was moved, <c>false</c> otherwise.</returns>
  327.     /// <param name="board">Board.</param>
  328.     public static bool moveUp(Tile[][] board){
  329.         //# """as moveLeft, except board flipped about the x/y diagonal during the move"""
  330.         //# result = False
  331.         bool result = false;
  332.         //# flipXY(board); result = moveLeft(board); flipXY(board)
  333.         flipXY(board); result = moveLeft(board); flipXY(board);
  334.         //# return result
  335.         return result;
  336.     }
  337.     //# def moveDown(board):
  338.     /// <summary>as moveLeft, except board flipped about the x/y diagonal AND horizontally</summary>
  339.     /// <returns><c>true</c>, if down was moved, <c>false</c> otherwise.</returns>
  340.     /// <param name="board">Board.</param>
  341.     public static bool moveDown(Tile[][] board){
  342.         //# """as moveLeft, except board flipped about the x/y diagonal AND horizontally"""
  343.         //# result = False
  344.         bool result = false;
  345.         //# flipXY(board); flipHorizontal(board); result = moveLeft(board);
  346.         flipXY(board); flipHorizontal(board); result = moveLeft(board);
  347.         //# flipHorizontal(board); flipXY(board)
  348.         flipHorizontal(board); flipXY(board);
  349.         //# return result
  350.         return result;
  351.     }
  352.     //# def isMergePossible(board):
  353.     public static bool isMergePossible(Tile[][] board){
  354.         //# height = len(board)
  355.         int height = board.Length;
  356.         //# width = len(board[0])
  357.         int width = board[0].Length;
  358.         //# for row in range(height-1):
  359.         for(int row=0; row < height-1; row++){
  360.             //# for col in range(width-1):
  361.             for(int col=0; col < width-1; col++){
  362.                 //# if board[row][col] != 0 and board[row][col] == board[row  ][col+1]:
  363.                 if(board[row][col] != null && board[row  ][col+1] == null
  364.                 && board[row][col].number == board[row  ][col+1].number) {
  365.                     //# return True
  366.                     return true;
  367.                 }
  368.                 //# if board[row][col] != 0 and board[row][col] == board[row+1][col  ]:
  369.                 if(board[row][col] != null && board[row+1][col  ] != null
  370.                 && board[row][col].number == board[row+1][col  ].number){
  371.                     //# return True
  372.                     return true;
  373.                 }
  374.             }
  375.         }
  376.         //# return False
  377.         return false;
  378.     }
  379.  
  380.     Tile[][] board;
  381.     //# gameIsRunning = True
  382.     public bool gameIsRunning = true;
  383.     //# moves = {'w':moveUp, 'a':moveLeft, 's':moveDown, 'd':moveRight}
  384.     Dictionary<char,moveFunction> moves = new Dictionary<char, moveFunction>{
  385.         {'w',moveUp}, {'a',moveLeft}, {'s',moveDown}, {'d',moveRight}
  386.     };
  387.     delegate bool moveFunction (Tile[][] board);
  388.     public static void Main (string[] args) {
  389.         //# # initialize the game
  390.         Game2048 g = new Game2048(); // creates an instance of our game
  391.         g.init();  // tells the game to initialize
  392.         g.run ();  // tells the game to run
  393.     }
  394.     public void init() {
  395.         //# board = makeBoard()
  396.         board = makeBoard ();
  397.         //# initializeBoardWith2RandomValues(board)
  398.         initializeBoardWith2RandomValues (board);
  399.     }
  400.     void draw() {
  401.         //# print("\033[1;1f") # tell console to move cursor to row1 col1
  402.         //Console.SetCursorPosition(0,0);
  403.         //# printBoard(board)
  404.         printBoard(board);
  405.     }
  406.     char handleInput() {
  407.         //# direction = keyhit.getch() #raw_input("wasd: ")
  408.         char direction = Console.ReadKey().KeyChar;
  409.         return direction;
  410.     }
  411.     public void update(char direction) {
  412.         //# actualThingHappend = False
  413.         bool actualThingHappened = false;
  414.         //# if direction in moves:
  415.         if(moves.ContainsKey(direction)){
  416.             //# actualThingHappend = moves[direction](board)
  417.             actualThingHappened = moves[direction](board);
  418.         }
  419.         //# if actualThingHappend:
  420.         if(actualThingHappened){
  421.             //# addToRandomSpot(board, random.choice([2,4]))
  422.             int[] n = {2,4};
  423.             addToRandomSpot(board, n[random.Next(n.Length)]);
  424.         }
  425.         //# if not actualThingHappend and len(findEmptySpots(board)) == 0 and not isMergePossible(board):
  426.         if(!actualThingHappened && findEmptySpots(board).Count == 0 && !isMergePossible(board)){
  427.             //# gameIsRunning = False
  428.             gameIsRunning = false;
  429.             //# print("GAME OVER")
  430.             Console.WriteLine("GAME OVER");
  431.         }
  432.         //# if direction == 'q': gameIsRunning = False
  433.         if(direction == 'q'){gameIsRunning = false;}
  434.     }
  435.     public void run() {
  436.         while(gameIsRunning){
  437.             draw ();
  438.             update (handleInput ());
  439.         }
  440.     }
  441.     public Coord WhereIs(Tile t) {
  442.         for (int row = 0; row < board.Length; row++) {
  443.             for (int col = 0; col < board [row].Length; col++) {
  444.                 if (board [row] [col] == t) {
  445.                     return new Coord (row, col);
  446.                 }
  447.             }
  448.         }
  449.         return null;
  450.     }
  451. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top