Skip to content

Commit 5ae3723

Browse files
Merge branch 'master' into mosaic
2 parents 5ae23a1 + 318c2fb commit 5ae3723

File tree

111 files changed

+13635
-73
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+13635
-73
lines changed

2048 GAME/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Project Name
2+
3+
This is a simple implementation of the classic game 2048 in Python. The objective of the game is to slide numbered tiles on a grid to combine them and create a tile with the number 2048. The game ends when the grid is full, and no more moves can be made, or when the player successfully creates the 2048 tile.
4+
5+
## How to Play (For Games/Apps)
6+
7+
Initialization: At the start of the game, an empty 4x4 grid is displayed with two randomly placed tiles, each having a value of 2.
8+
9+
Controls: Use the following keys to control the game:
10+
11+
'W' or 'w': Move Up
12+
'S' or 's': Move Down
13+
'A' or 'a': Move Left
14+
'D' or 'd': Move Right
15+
Gameplay: The game progresses with each move in the direction specified by the player. All tiles slide in the specified direction until they reach the grid's edge or collide with another tile of the same value. If two tiles of the same value collide, they merge into a single tile with double the value. After each move, a new tile with a value of 2 is added to the grid at a random empty cell.
16+
17+
Winning Condition: The game is won when a tile with the value of 2048 is created.
18+
19+
Losing Condition: The game is lost when the grid is full, and no more valid moves can be made.
20+
21+
## Features
22+
23+
Basic 2048 gameplay with grid and tile manipulation.
24+
Random tile generation after each move.
25+
Game over and win conditions detection.
26+
Simple console-based interface.
27+
28+
## Technologies Used
29+
30+
Python programming language.

2048 GAME/game.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# 2048.py
2+
3+
# importing the logic.py file
4+
# where we have written all the
5+
# logic functions used.
6+
import logic
7+
8+
# Driver code
9+
if __name__ == '__main__':
10+
11+
# calling start_game function
12+
# to initialize the matrix
13+
mat = logic.start_game()
14+
15+
while(True):
16+
17+
# taking the user input
18+
# for next step
19+
x = input("Press the command : ")
20+
21+
# we have to move up
22+
if(x == 'W' or x == 'w'):
23+
24+
# call the move_up function
25+
mat, flag = logic.move_up(mat)
26+
27+
# get the current state and print it
28+
status = logic.get_current_state(mat)
29+
print(status)
30+
31+
# if game not over then continue
32+
# and add a new two
33+
if(status == 'GAME NOT OVER'):
34+
logic.add_new_2(mat)
35+
36+
# else break the loop
37+
else:
38+
break
39+
40+
# the above process will be followed
41+
# in case of each type of move
42+
# below
43+
44+
# to move down
45+
elif(x == 'S' or x == 's'):
46+
mat, flag = logic.move_down(mat)
47+
status = logic.get_current_state(mat)
48+
print(status)
49+
if(status == 'GAME NOT OVER'):
50+
logic.add_new_2(mat)
51+
else:
52+
break
53+
54+
# to move left
55+
elif(x == 'A' or x == 'a'):
56+
mat, flag = logic.move_left(mat)
57+
status = logic.get_current_state(mat)
58+
print(status)
59+
if(status == 'GAME NOT OVER'):
60+
logic.add_new_2(mat)
61+
else:
62+
break
63+
64+
# to move right
65+
elif(x == 'D' or x == 'd'):
66+
mat, flag = logic.move_right(mat)
67+
status = logic.get_current_state(mat)
68+
print(status)
69+
if(status == 'GAME NOT OVER'):
70+
logic.add_new_2(mat)
71+
else:
72+
break
73+
else:
74+
print("Invalid Key Pressed")
75+
76+
# print the matrix after each
77+
# move.
78+
print(mat)

2048 GAME/logic.py

+255
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
# logic.py to be
2+
# imported in the 2048.py file
3+
4+
# importing random package
5+
# for methods to generate random
6+
# numbers.
7+
import random
8+
9+
# function to initialize game / grid
10+
# at the start
11+
def start_game():
12+
13+
# declaring an empty list then
14+
# appending 4 list each with four
15+
# elements as 0.
16+
mat =[]
17+
for i in range(4):
18+
mat.append([0] * 4)
19+
20+
# printing controls for user
21+
print("Commands are as follows : ")
22+
print("'W' or 'w' : Move Up")
23+
print("'S' or 's' : Move Down")
24+
print("'A' or 'a' : Move Left")
25+
print("'D' or 'd' : Move Right")
26+
27+
# calling the function to add
28+
# a new 2 in grid after every step
29+
add_new_2(mat)
30+
return mat
31+
32+
# function to add a new 2 in
33+
# grid at any random empty cell
34+
def add_new_2(mat):
35+
36+
# choosing a random index for
37+
# row and column.
38+
r = random.randint(0, 3)
39+
c = random.randint(0, 3)
40+
41+
# while loop will break as the
42+
# random cell chosen will be empty
43+
# (or contains zero)
44+
while(mat[r] != 0):
45+
r = random.randint(0, 3)
46+
c = random.randint(0, 3)
47+
48+
# we will place a 2 at that empty
49+
# random cell.
50+
mat[r] = 2
51+
52+
# function to get the current
53+
# state of game
54+
def get_current_state(mat):
55+
56+
# if any cell contains
57+
# 2048 we have won
58+
for i in range(4):
59+
for j in range(4):
60+
if(mat[i][j]== 2048):
61+
return 'WON'
62+
63+
# if we are still left with
64+
# atleast one empty cell
65+
# game is not yet over
66+
for i in range(4):
67+
for j in range(4):
68+
if(mat[i][j]== 0):
69+
return 'GAME NOT OVER'
70+
71+
# or if no cell is empty now
72+
# but if after any move left, right,
73+
# up or down, if any two cells
74+
# gets merged and create an empty
75+
# cell then also game is not yet over
76+
for i in range(3):
77+
for j in range(3):
78+
if(mat[i][j]== mat[i + 1][j] or mat[i][j]== mat[i][j + 1]):
79+
return 'GAME NOT OVER'
80+
81+
for j in range(3):
82+
if(mat[3][j]== mat[3][j + 1]):
83+
return 'GAME NOT OVER'
84+
85+
for i in range(3):
86+
if(mat[i][3]== mat[i + 1][3]):
87+
return 'GAME NOT OVER'
88+
89+
# else we have lost the game
90+
return 'LOST'
91+
92+
# all the functions defined below
93+
# are for left swap initially.
94+
95+
# function to compress the grid
96+
# after every step before and
97+
# after merging cells.
98+
def compress(mat):
99+
100+
# bool variable to determine
101+
# any change happened or not
102+
changed = False
103+
104+
# empty grid
105+
new_mat = []
106+
107+
# with all cells empty
108+
for i in range(4):
109+
new_mat.append([0] * 4)
110+
111+
# here we will shift entries
112+
# of each cell to it's extreme
113+
# left row by row
114+
# loop to traverse rows
115+
for i in range(4):
116+
pos = 0
117+
118+
# loop to traverse each column
119+
# in respective row
120+
for j in range(4):
121+
if(mat[i][j] != 0):
122+
123+
# if cell is non empty then
124+
# we will shift it's number to
125+
# previous empty cell in that row
126+
# denoted by pos variable
127+
new_mat[i][pos] = mat[i][j]
128+
129+
if(j != pos):
130+
changed = True
131+
pos += 1
132+
133+
# returning new compressed matrix
134+
# and the flag variable.
135+
return new_mat, changed
136+
137+
# function to merge the cells
138+
# in matrix after compressing
139+
def merge(mat):
140+
141+
changed = False
142+
143+
for i in range(4):
144+
for j in range(3):
145+
146+
# if current cell has same value as
147+
# next cell in the row and they
148+
# are non empty then
149+
if(mat[i][j] == mat[i][j + 1] and mat[i][j] != 0):
150+
151+
# double current cell value and
152+
# empty the next cell
153+
mat[i][j] = mat[i][j] * 2
154+
mat[i][j + 1] = 0
155+
156+
# make bool variable True indicating
157+
# the new grid after merging is
158+
# different.
159+
changed = True
160+
161+
return mat, changed
162+
163+
# function to reverse the matrix
164+
# means reversing the content of
165+
# each row (reversing the sequence)
166+
def reverse(mat):
167+
new_mat =[]
168+
for i in range(4):
169+
new_mat.append([])
170+
for j in range(4):
171+
new_mat[i].append(mat[i][3 - j])
172+
return new_mat
173+
174+
# function to get the transpose
175+
# of matrix means interchanging
176+
# rows and column
177+
def transpose(mat):
178+
new_mat = []
179+
for i in range(4):
180+
new_mat.append([])
181+
for j in range(4):
182+
new_mat[i].append(mat[j][i])
183+
return new_mat
184+
185+
# function to update the matrix
186+
# if we move / swipe left
187+
def move_left(grid):
188+
189+
# first compress the grid
190+
new_grid, changed1 = compress(grid)
191+
192+
# then merge the cells.
193+
new_grid, changed2 = merge(new_grid)
194+
195+
changed = changed1 or changed2
196+
197+
# again compress after merging.
198+
new_grid, temp = compress(new_grid)
199+
200+
# return new matrix and bool changed
201+
# telling whether the grid is same
202+
# or different
203+
return new_grid, changed
204+
205+
# function to update the matrix
206+
# if we move / swipe right
207+
def move_right(grid):
208+
209+
# to move right we just reverse
210+
# the matrix
211+
new_grid = reverse(grid)
212+
213+
# then move left
214+
new_grid, changed = move_left(new_grid)
215+
216+
# then again reverse matrix will
217+
# give us desired result
218+
new_grid = reverse(new_grid)
219+
return new_grid, changed
220+
221+
# function to update the matrix
222+
# if we move / swipe up
223+
def move_up(grid):
224+
225+
# to move up we just take
226+
# transpose of matrix
227+
new_grid = transpose(grid)
228+
229+
# then move left (calling all
230+
# included functions) then
231+
new_grid, changed = move_left(new_grid)
232+
233+
# again take transpose will give
234+
# desired results
235+
new_grid = transpose(new_grid)
236+
return new_grid, changed
237+
238+
# function to update the matrix
239+
# if we move / swipe down
240+
def move_down(grid):
241+
242+
# to move down we take transpose
243+
new_grid = transpose(grid)
244+
245+
# move right and then again
246+
new_grid, changed = move_right(new_grid)
247+
248+
# take transpose will give desired
249+
# results.
250+
new_grid = transpose(new_grid)
251+
return new_grid, changed
252+
253+
# this file only contains all the logic
254+
# functions to be called in main function
255+
# present in the other file

Advisor_App/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# ADVISOR APPLICATION
2+
3+
This is a Python Script which gives you advice and motivation quote lines.
4+
5+
---
6+
7+
## Requirements
8+
9+
For this script to run you need to have requests packages installed
10+
11+
Run the command in terminal to install package
12+
13+
```
14+
$ pip install requests
15+
```
16+
```
17+
$ pip install tkinter
18+
```
19+
Run the program using command
20+
21+
```
22+
$ python advice.py
23+
```
24+
25+
## Created By
26+
27+
[Avdhesh Varshney](https://github.com/Avdhesh-Varshney)
28+
29+
<br>
30+
31+
**Thanks for using this program**
32+

0 commit comments

Comments
 (0)