Professional Documents
Culture Documents
블로그
카 가속의 블로그
2048.xlsm
(버그 수정했습니다(2))
(2021.02.21 보드판에 1이나 2가 남아있음에도 2, 4와 같은 상위 숫자가 생성되는 문제 해결)
우선 틀을 만든다
해야할 것은
2048 보드판인
B2 ~ E5 셀을 굵은 외곽 테두리로 지정해주고
가운데 정렬
글씨 크기 20? 정도로 설정해둔다
셀의 색은 설정하지 않아도 알아서 된다.
Sub START()
Range("B2:E5").Select
Selection.ClearContents
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Interior.Color = RGB(255, 255, 255)
Cells(6, 8) = 0
Cells(2, 6) = "0단계"
Range("A1").Select
Application.OnKey "{UP}", "위"
Application.OnKey "{DOWN}", "아래"
Application.OnKey "{LEFT}", "왼쪽"
Application.OnKey "{RIGHT}", "오른쪽"
End Sub
게임 시작 프로시저
A1셀을 선택해두는 이유는 셀 선택할 때 생기는 테두리가 지저분하기 때문에 구석으로 치웠음
application.onkey "해당키","매크로이름"
Sub game_over_condition()
Dim count1, count2
count1 = 0
count2 = 0
If count1 = 16 Then
If count2 = 16 Then
game_over
End If
End If
End Sub
게임 오버 조건 확인 프로시저
Sub game_over()
Range("B2,C3,D4,E5").Select ' \
With Selection.Borders(xlDiagonalDown)
.LineStyle = xlcontinous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Range("E2,D3,C4,B5").Select ' /
With Selection.Borders(xlDiagonalUp)
.LineStyle = xlcontinous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
게임 오버 프로시저
Sub 얇게()
Range("B2:E5").Select
Selection.Font.Bold = False
Range("A1").Select
End Sub
글씨 얇게 하는 프로시저
Sub 색깔()
For i = 2 To 5
For j = 2 To 5
Select Case Cells(i, j)
Case 0
Cells(i, j).Interior.Color = RGB(255, 255, 255)
Case 1
Cells(i, j).Interior.Color = RGB(255, 101, 101)
Case 2
Cells(i, j).Interior.Color = RGB(255, 163, 101)
Case 4
Cells(i, j).Interior.Color = RGB(255, 255, 101)
Case 8
Cells(i, j).Interior.Color = RGB(25, 255, 25)
Case 16
Cells(i, j).Interior.Color = RGB(127, 127, 25)
Case 32
Cells(i, j).Interior.Color = RGB(116, 116, 208)
Case 64
Cells(i, j).Interior.Color = RGB(255, 63, 255)
Case 128
Cells(i, j).Interior.Color = RGB(127, 127, 127)
Case 256
Cells(i, j).Interior.Color = RGB(255, 0, 0)
Case 512
Cells(i, j).Interior.Color = RGB(255, 102, 0)
Case 1024
Cells(i, j).Interior.Color = RGB(255, 255, 0)
Case 2048
Cells(i, j).Interior.Color = RGB(23, 181, 94)
Case 4096
Cells(i, j).Interior.Color = RGB(0, 0, 255)
End Select
Next
Next
End Sub
셀 배경색 프로시저
Sub 생성()
얇게
Dim rd
Dim mm
Dim isThereOne
Dim isThereTwo
Dim isThereFour
For i = 1 To 1000
rd = WorksheetFunction.RandBetween(0, 15)
isThereOne = 0
For k = 2 To 5
For j = 2 To 5
If Cells(k, j) = 1 Then
isThereOne = isThereOne + 1 ' 1의 개수 체크
End If
Next
Next
isThereTwo = 0
If mm <= 2 Then
For k = 2 To 5
For j = 2 To 5
If Cells(k, j) = 1 Then
isThereOne = isThereOne + 1 ' 1의 개수 체크
ElseIf Cells(k, j) = 2 Then
isThereTwo = isThereTwo + 1 ' 2의 개수 체크
End If
Next
Next
If isThereOne = 1 Then
Cells(Int(rd / 4) + 2, (rd Mod 4) + 2) = 1 ' 1,2 유무에 따라 생성
ElseIf isThereTwo = 1 Then
Cells(Int(rd / 4) + 2, (rd Mod 4) + 2) = 2
Else
Cells(Int(rd / 4) + 2, (rd Mod 4) + 2) = 4
End If
isThereOne = 0
isThereTwo = 0
For k = 2 To 5
For j = 2 To 5
If Cells(k, j) = 1 Then ' 1의 개수 체크
isThereOne = isThereOne + 1
ElseIf Cells(k, j) = 2 Then ' 2의 개수 체크
isThereTwo = isThereTwo + 1
End If
Next
Next
isThereOne = 0
isThereTwo = 0
isThereFour = 0
For k = 2 To 5
For j = 2 To 5
If Cells(k, j) = 1 Then
isThereOne = isThereOne + 1
ElseIf Cells(k, j) = 2 Then
isThereTwo = isThereTwo + 1
ElseIf Cells(k, j) = 4 Then
isThereFour = isThereFour + 1
End If
Next
Next
If isThereOne = 1 Then
Cells(Int(rd / 4) + 2, (rd Mod 4) + 2) = 1
ElseIf isThereTwo = 1 Then
Cells(Int(rd / 4) + 2, (rd Mod 4) + 2) = 2
ElseIf isThereFour = 1 Then
Cells(Int(rd / 4) + 2, (rd Mod 4) + 2) = 4
End If
Next
Cells(Int(rd / 4) + 2, (rd Mod 4) + 2).Select
Selection.Font.Bold = True '글씨 굵게
game_over_condition
End Sub
숫자 랜덤 생성 프로시저
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
이 순으로 칸이 배정된다.
6,8 셀은 점수 셀이다
Select Case cells(6,8)을 통해 점수를 토대로 몇 단계인지 식별하고
그에 맞는 숫자가 나온다.
이 프로시저를 통해 난이도 조절이 가능하다.
Sub 위()
For a = 1 To 3 '위로 올리기
For i = 1 To 3
For j = 2 To 5
If Cells(5 - i, j) = 0 Then
Cells(5 - i, j) = Cells(6 - i, j)
Cells(6 - i, j).ClearContents
End If
Next
Next
Next
밀기 프로시저
위로 미는 프로시저이다.
a = 1 to 3은
보드판이 4x4 이므로
맨 아래에서 맨 위로 올라갈 경우도 생각해 설정했다.
만약 위가 비어있다면 쭉 쭉 올라갈 것이고
차 있다면 그냥 반복될 뿐 아무 의미 없어진다.
i가1
j가 2 일 때를 가정하면
If Cells(5 - i, j) = 0 Then
Cells(5 - i, j) = Cells(6 - i, j)
Cells(6 - i, j).ClearContents
는
If Cells(4, 2) = 0 Then
Cells(4, 2) = Cells(5, 2)
Cells(5, 2).ClearContents 이렇게 되는데
위에 셀이 비어있다면 해당 셀의 값을 주고 해당 셀의 값은 없앤다.
i가 2
j가 2 일 때 가정