Four-color three-elimination game algorithm

Source: Internet
Author: User

Four-color three-elimination game algorithm


The following is a python-written four-color three-elimination game algorithm, it is easy to change into more colors and ranks. The basic idea is that 3 of the same diamonds together can be eliminated. Nonsense not to say, on the code:

#!/usr/bin/python#-*-Coding:utf-8-*-#====================================================================== Import OS import sysimport getoptimport timeimport random#========================================================== ============# Color Output#def perror (s): print ' \033[31m[error]%s\033[31;m '% (s) def pinfo (s): print ' \033[32m[inf O]%s\033[32;m '% (s) def Pwarn (s): print ' \033[33m[warn]%s\033[33;m '% (s) #------------------------------------------- ---------------def Red (): print ' \033[31ma\033[31;m ', Def Yellow (): print ' \033[33mb\033[33;m ', Def Blue (): print ' \033[34mc\033[34;m ', Def Green (): print ' \033[32md\033[32;m ', Def one (): print ' \033[31m1\033[31;m ', Def Zero (): PRI NT ' O ', Def pout (C): If c== ' A ': Red () elif c== ' B ': yellow () elif c== ' C ': Blue () elif C = = ' D ': Green () Else:zero () #==========================================================# 4 colors and 3 cr Ash for 5x7 Diamonds tablecrash3_cols = 7CRash3_rows = 5crash3_colors = [' O ', ' A ', ' B ', ' C ', ' D ']crash3_diamonds_table = [[0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0    , 0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0],]crash3_diamonds_result = [[0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0],]def crash3_print_colors (table): For row in range (0, crash3_ ROWS, 1): For Col in range (0, Crash3_cols, 1): color = Table[row][col] Pout (crash3_colors[col or]) print ' Def crash3_print_values (table): For row in range (0, crash3_rows, 1): For Col in range (0, CRA Sh3_cols, 1): v = table[row][col] if v = = 1:one () else:ze RO () print ' Def crash3_reset_value (table, V): For row in range (0, crash3_rows, 1): For Col in range (0, C Rash3_cols, 1): table[row][col] = vdef crash3_init_table (table): print ("\ n----------------------\ncrash3_ini t_table:\n----------------------") color = Random.randint (1, 4) for row in range (0, crash3_rows, 1): For Col in range (0, Crash3_cols, 1): color = Random.randint (1, 4) table[row][col] = colordef crash3_on_cell (table, result, row, col) : If col < crash3_cols-2: (a,b,c) = (Table[row][col], table[row][col+1], table[row][col+2]) if a==b            and B==c:result[row][col] = 0;            Result[row][col+1] = 0;    RESULT[ROW][COL+2] = 0; If row < crash3_rows-2: (a,b,c) = (Table[row][col], Table[row+1][col], Table[row+2][col]) if A==b and b            ==c:result[row][col] = 0;            Result[row+1][col] = 0; Result[row+2][col] = 0;def crash3_on_trigger (table, result): print ("\ n----------------------\ncrash3_on_trigger:\ N----------------------") for row in range (0, crash3_rows, 1): For Col in range (0, Crash3_cols, 1): C Rash3_on_cell (table, result, row, col) #==========================================================# Main () entryif __name__ = = "__main__": pinfo ("Crash linked 3 Diamonds.\ncopyright by cheungmin    E, all rights reserved! ") Crash3_init_table (crash3_diamonds_table) crash3_print_colors (crash3_diamonds_table) Crash3_reset_value (CRASH3_ Diamonds_result, 1) crash3_on_trigger (crash3_diamonds_table, Crash3_diamonds_result) crash3_print_values (CRASH3_     Diamonds_result)

Run results, O for elimination, 1 for no elimination:

In practical use, it is easy to change to another language.

Four-color three-elimination game algorithm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.