Skip to content

Instantly share code, notes, and snippets.

@chmodsss
Created August 29, 2018 22:58
Show Gist options
  • Save chmodsss/615b82234f6cf35fe5deab6d6e2e9bfe to your computer and use it in GitHub Desktop.
Save chmodsss/615b82234f6cf35fe5deab6d6e2e9bfe to your computer and use it in GitHub Desktop.
tetris.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "tetris.ipynb",
"version": "0.3.2",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"[View in Colaboratory](https://colab.research.google.com/gist/sivu1/615b82234f6cf35fe5deab6d6e2e9bfe/tetris.ipynb)"
]
},
{
"metadata": {
"id": "9AxrnJmxO7q6",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"import numpy as np\n",
"import random, time\n",
"from IPython.display import clear_output"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "K65CE9A8PF92",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"class Tetris:\n",
" def __init__(self):\n",
" self.border = np.zeros((7,7))\n",
" self.borderback = self.border.copy()\n",
" self.recshape = np.array([[1,1,1,1]])\n",
" self.sshape = np.array([[0,1],[1,1],[1,0]])\n",
" self.stshape = np.array([[1,0],[1,1],[0,1]])\n",
" self.squshape = np.array([[1,1],[1,1]])\n",
" self.tshape = np.array([[0,1,0],[1,1,1]])\n",
" self.lshape = np.array([[1,1],[1,0],[1,0]])\n",
" self.ltshape = np.array([[1,1],[0,1],[0,1]])\n",
" \n",
" self.allshapes = [self.recshape, self.squshape, self.sshape, self.stshape, \n",
" self.tshape, self.lshape, self.ltshape]\n",
" \n",
" self.runProgram = True\n",
" \n",
" \n",
" def getspace(self):\n",
" return np.sum(self.borderback, axis=0)\n",
"\n",
" def getpose(self, box, piece):\n",
" fitspace = []\n",
" for i in range(len(box) - len(piece)+1):\n",
" boxcopy = box.copy()\n",
" boxcopy[i:i+len(piece)] += piece\n",
" fitspace.append(boxcopy)\n",
" boxheight = np.max(fitspace, axis=1)\n",
" position = np.where(boxheight == min(boxheight))\n",
" return position[0][0]\n",
"\n",
"\n",
" def setpiece(self, piece):\n",
" (px,py) = piece.shape\n",
" (bx,by) = self.border.shape\n",
" spaceleft = self.getspace()\n",
" pieceback = np.ones(piece.shape)\n",
" pieceweight = np.sum(pieceback, axis=0)\n",
" xpose = self.getpose(spaceleft, pieceweight)\n",
" ypose = int(spaceleft[xpose])\n",
" if bx-(px+ypose) >= 0:\n",
" self.border[bx-(px+ypose):bx-ypose, xpose:py+xpose] = piece\n",
" self.borderback[bx-(px+ypose):bx-ypose, xpose:py+xpose] = pieceback\n",
" else:\n",
" print(\"No space left. End of program....\")\n",
" self.runProgram = False\n",
"\n",
" def runtetris(self,piece):\n",
" self.setpiece(piece)\n",
" print(self.border)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "2-EPzLjKPEOe",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 463
},
"outputId": "1e401f90-c9ab-49b7-8192-258578f851c7"
},
"cell_type": "code",
"source": [
"for sh in Tetris().allshapes:\n",
" print(sh)\n",
" print('-------------')"
],
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": [
"[[1 1 1 1]]\n",
"-------------\n",
"[[1 1]\n",
" [1 1]]\n",
"-------------\n",
"[[0 1]\n",
" [1 1]\n",
" [1 0]]\n",
"-------------\n",
"[[1 0]\n",
" [1 1]\n",
" [0 1]]\n",
"-------------\n",
"[[0 1 0]\n",
" [1 1 1]]\n",
"-------------\n",
"[[1 1]\n",
" [1 0]\n",
" [1 0]]\n",
"-------------\n",
"[[1 1]\n",
" [0 1]\n",
" [0 1]]\n",
"-------------\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "xIgacOwqYZMn",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 240
},
"outputId": "d8165814-f531-4f0c-aed6-4b6899ccb184"
},
"cell_type": "code",
"source": [
"t = Tetris()\n",
"while t.runProgram:\n",
" piece = random.choice(t.allshapes)\n",
" clear_output()\n",
" print(\"Incoming piece : \\n\", piece)\n",
" t.runtetris(piece)\n",
" time.sleep(2)"
],
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": [
"Incoming piece : \n",
" [[1 1]\n",
" [0 1]\n",
" [0 1]]\n",
"No space left. End of program....\n",
"[[1. 1. 1. 1. 0. 0. 1.]\n",
" [0. 1. 0. 0. 1. 1. 1.]\n",
" [1. 1. 1. 1. 1. 1. 0.]\n",
" [0. 1. 0. 1. 0. 1. 1.]\n",
" [1. 1. 1. 1. 1. 1. 1.]\n",
" [0. 1. 0. 0. 1. 1. 1.]\n",
" [1. 1. 1. 0. 1. 1. 1.]]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "eOyohgSV6AfF",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment