일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 백준 18428
- 자소서 빨리 쓰는 법
- hadoop safe mode
- hive beeline 실행
- Could not open client transport with JDBC Uri: jdbc:hive2://localhost:10000
- mac hive 3
- Resources are low on NN
- 이더리움 #ethereum
- hive beeline 에러
- mac hadoop 3
- hive beeline
- 카카오 2020 코딩테스트
- mac hive
- 카카오 자물쇠와 열쇠
- 자소서 너무 오래 걸림
- Failed to connect to localhost:10000
- 기업 조사 빨리 하는 법
- mac hadoop 설정
- 자소서 빨리
- 이더리움
- code=0)
- Safe mode is ON
- mac hadoop 설치
- hive beeline 설정
- 도커 교과서
- 자소서 시간 줄이기
- hadoop safe mode leave
- is not allowed to impersonate hive (state=08S01
- 카카오 2020 코테
- mac hadoop
Archives
- Today
- Total
A seeker after truth
문제5 본문
사정상 문제 출처 밝히지 않음.
파이썬 객체지향에 대해 잘 이해할 수 있는 계기가 되는 문제였다.
특히 예전에 클래스변수를 어떻게 해야할지 잘 몰라서 틀렸었는데, 이번 방학에 공부한 내용을 바탕으로 잘 해결할 수 있었다.
참, randint(int,int)범위가 바꼈더라. 양쪽 두 숫자가 모두 부등식에서 <,>가 아닌 <= 즉 이퀄이 포함된 부등호 의미랑 같게 되었다. 즉 초과/미만 이 아니고, 이상/이하 의미가 되었다. 원래는 이상/미만 의미였음.
같은 응용력을 문제 4에서도 썼다: (self.f)로 다른 메서드에서도 f를 사용할 수 있다는 점, 그리고 생성자가 아니라도 self.~ 식으로 필드(파이썬에선 attribute 였나?)를 사용할 수 있다는 점 말이다.
# 특이 사항: 파일에 기록하는 부분은 vscode에선 안됨. 파이참으로도 해봐야할듯
from random import randint
from csv import writer
listValue = [ 0, 1, 2 ] # 0 is rock, 1 is paper, 2 is scissor
listDescription = [ "rock", "paper", "scissor" ]
def displayGameResult(player1, givenGamer1, player2, givenGamer2, givenWinner):
if (givenGamer1 != -1) and (givenGamer2 != -1) and (givenWinner != -1):
msg = "[" + player1.getId() + ":" + listDescription[givenGamer1] + "] vs [" + player2.getId() + ":" + listDescription[givenGamer2] + "]"
if winner == 1:
msg += " -> winner is " + player1.getId()
elif winner == 2:
msg += " -> winner is " + player2.getId()
else:
msg += " -> tie game"
print(msg)
else:
print("Game session not ready")
# ANSWER : START
class Player:
whatvalue = None
def __init__(self, playerID="noname"):
self.playerID = playerID
def getId(self):
return self.playerID
def setValue(self, value):
if value == None: self.whatvalue = randint(0,2)
else: self.whatvalue = value # 멤버 데이터 저장이면 클래스 밖이나 생성자 안에 있어야 되지 않나...
def getValue(self):
return self.whatvalue # 이게 가능한지 모르겠음. 생성자 안에서 선언 안했으면 밖에서라도 선언해야하는게 아닌가 해서... 파썬에서 이해가 안되는 부분은 필드에 대한 것인듯...
class Game:
winner = None
gamelist = None
def __init__(self, player1, player2, gamename):
try:
self.player1 = player1
self.player2 = player2
self.gamename = gamename
except:
# 게임 비정상 상태 정의. 근데 이거 어떻게 하지?!
raise Exception("Session Error: Abnormal State")
def runGame(self, value1 = randint(0,2), value2 = randint(0,2)):
try:
# decideWinner에서 실행해도 값이 당연히 변경되지 않고 잘 반환되는 구조라고 할 수 있.
self.player1.setValue(value1)
self.player2.setValue(value2)
return value1, value2
except: return (-1,-1)
def decideWinner(self): # value1,2를 걍 이용
value1, value2 = self.runGame()
try:
if (value1 == 0): # rock
if(value2 == 0): # rock
self.winner = 0
elif(value2 == 1): # paper
self.winner = 2
else:
self.winner = 1
elif (value1 == 1): # paper
if(value2 == 0): # rock
self.winner = 1
elif(value2 == 2): # scissor
self.winner = 2
else:
self.winner = 0
else: # scissor
if(value2 == 0):
self.winner = 2
elif(value2 == 1):
self.winner = 1
else:
self.winner = 0
return self.winner
except: return -1
def logGame(self):
try:
self.gamelist = [self.player1.playerID, self.player1.whatvalue, self.player2.playerID, self.player2.whatvalue, winner]
return True
except: return False
def closeSession(self):
try:
with open("sample.csv",'w') as f:
wr = csv.writer(f)
wr.writerow(["sequence","player1","player1-turn","player2","player2-turn","winner"])
wr.writerow(self.gamelist)
except: return False
# ANSWER : END
player1 = Player("20190001")
player2 = Player("20190002")
game = Game(player1, player2, "myGame")
gamer1, gamer2 = game.runGame()
winner = game.decideWinner()
game.logGame()
displayGameResult(player1, gamer1, player2, gamer2, winner)
gamer1, gamer2 = game.runGame(listValue[0], listValue[1])
winner = game.decideWinner()
game.logGame()
displayGameResult(player1, gamer1, player2, gamer2, winner)
game.closeSession()
'Algorithm > 문제풀이' 카테고리의 다른 글
exercism reverse-string solution (0) | 2020.01.24 |
---|---|
프로그래머스 정렬 문제풀이(1) (0) | 2020.01.24 |
문제4 (0) | 2020.01.08 |
파이썬 수업 문제 분석 3 (0) | 2020.01.07 |
수업 퀴즈 문제 분석 2 (0) | 2020.01.02 |