d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need A Little Help In Python > A Mastermind Game
Add Reply New Topic New Poll
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Apr 22 2014 08:25pm
Well don't need help with the entire game. I'm a c# programmer, but I have to make thin game in python.
The game in simple. The computer choose 5 different color from 8 colors and in a specifid order.
Then the player have to guess the 5 colors in the right order.

here is the code:

Code
import random
TCool = ['NOI', 'BLA', 'JAU', 'ORA', 'ROU', 'VER', 'BRU', 'BLE']
TSol = []
Ttry = []
rand = 0
while i < 5:
rand = random.randint(0,7)
Tsol[i] = TCool[rand]
i = i +1
test = raw_input("Enter your five color in that format: co1,co2,co3,co4,co5")


I would like to know how to trim string test to make sure each color have the same format than TCool and of course 5 colors have to be entered and will be in Ttry array to compare with Tsol

This post was edited by eric838 on Apr 22 2014 08:26pm
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Apr 22 2014 08:56pm
Terrible code incoming

Code
from random import sample as fgt

def gen_color():
tcool = ['NOI', 'BLA', 'JAU', 'ORA', 'ROU', 'VER', 'BRU', 'BLE']
i = 0
while i < 5:
for e in tcool:
return fgt(tcool, 5)


def format(test, computer_color):
test = test.split()
computer_color = [y.lower() for y in computer_color]
test = [x.lower() for x in test]
if set(test) == set(computer_color):
return test
else:
'Lol u suk'

computer_color = gen_color()
test = raw_input('Enter five colors ')
format(test, computer_color)


It is not finished, but should get you started.

You need something to check if they're in order, if they match, or whatever.

Set() is not going to work, was just using it for testing and not really sure what format you want them in.


This post was edited by killg0re on Apr 22 2014 09:02pm
Member
Posts: 582
Joined: Mar 22 2014
Gold: 0.00
Apr 22 2014 09:03pm
Code
import random
import sys

TCool = ['NOI', 'BLA', 'JAU', 'ORA', 'ROU', 'VER', 'BRU', 'BLE']
TSol = [random.choice(TCool) for i in xrange(5)]
Ttry = []

test=[]
while len(test)<5:
print 'Enter your five color in that format: co1,co2,co3,co4,co5'
test = [color[0:3].upper() for color in sys.stdin.readline().strip().split(',') if color[0:3].upper() in TCool]
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Apr 23 2014 03:27pm
Quote (killg0re @ 22 Apr 2014 21:56)
Terrible code incoming

Code
from random import sample as fgt

def gen_color():
tcool = ['NOI', 'BLA', 'JAU', 'ORA', 'ROU', 'VER', 'BRU', 'BLE']
i = 0
while i < 5:
  for e in tcool:
  return fgt(tcool, 5)
 

def format(test, computer_color):
test = test.split()
computer_color = [y.lower() for y in computer_color]
test = [x.lower() for x in test]
if set(test) == set(computer_color):
  return test
else:
  'Lol u suk'

computer_color = gen_color()
test = raw_input('Enter five colors ')
format(test, computer_color)


It is not finished, but should get you started.

You need something to check if they're in order, if they match, or whatever.

Set() is not going to work, was just using it for testing and not really sure what format you want them in.



have to check player color with pc colors if matching or not
Member
Posts: 582
Joined: Mar 22 2014
Gold: 0.00
Apr 23 2014 03:32pm
Quote (eric838 @ Apr 23 2014 05:27pm)
have to check player color with pc colors if matching or not


My solution does exactly what you described.
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Apr 23 2014 05:33pm
Quote (poodaH @ 23 Apr 2014 16:32)
My solution does exactly what you described.


I mean after player picks 5 colors Ttry[] is compared with Tsol[], try will be incremented and a message will display if he got the right color at the good place or good color and wrong place

its looks like purple place game that come with win7 but with 8 colors and must match in right position

but why
Code
len(test)<5:


This post was edited by eric838 on Apr 23 2014 05:38pm
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Apr 28 2014 05:16pm
need help for that masterming game still have some bugs and user must quit game anytime

here is the code:

Code
import random
import sys
endprogram = 0

while endprogram = 0
print "There is the mastermind game!! \nYou have to pick 5 colors in the right order\n\n"
print "Chosse between two options:\n\n"
print "1- Play vs computer\n"
print "2- Computer guess your colors"

choix = input("Enter your choice")

if choix == 1:
print 'You choose a game vs computer'
elif choix == 2:
print 'Computer must guess your colorl'

#choix == 1 only
TCool = ['NOI', 'BLA', 'JAU', 'ORA', 'ROU', 'VER', 'BRU', 'BLE']
TSol = [random.choice(TCool) for i in xrange(5)]
win = 0

while win == 0
nbessai = 0
bcoolmplace, bcoolbplace = 0, 0
exitLoop = 0
TTSol = TSol #ttsol is a temp list of solution
test = []
while len(test) < 5:
print "Enter your five color in that format: co1,co2,co3,co4,co5"
test = [color[0:3].upper() for color in sys.stdin.readline().strip().split(',') if color[0:3].upper() in TCool]

nbessai = nbessai + 1;
for i in xrange(5):
if test[i] == TSol[i]:
test[i], TSol[i]= 'xxx', 'yyy'
bcoolbplace = bcoolbplace + 1

if bcoolbplace == 5
print 'Congratulation you won in {0}'.format(nbessai)
win = 1
else
i, j = 0, 0
while j < 5
if TTsol[j]
while i < 5 and exitLoop == 0
if test[i] == TTSol[j]
test[i], TTSol[j] ='xxx', 'yyy'
exitLoop = 1
i = i + 1
i = 0
exitLoop = 0
j = j + 1

print '{0} right color and right place and {1} right color in wrong place'.format(bcoolbplac

continu = ""
while continu != "y" and continu != "n"
continu = raw_input("Another game? y/n")
if continu = "y"
endprogram = 1


This post was edited by eric838 on Apr 28 2014 05:20pm
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Apr 28 2014 07:19pm
corrected some mistakes result at 1st try are good but wrong at second
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll