Game Of Life,

Dependencies:   N5110 mbed

Committer:
MsTee
Date:
Wed Jun 15 18:42:14 2016 +0000
Revision:
0:dc77cc9514ca
Game Of Life

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MsTee 0:dc77cc9514ca 1 ,
MsTee 0:dc77cc9514ca 2 #include "mbed.h"
MsTee 0:dc77cc9514ca 3 #include "N5110.h"
MsTee 0:dc77cc9514ca 4
MsTee 0:dc77cc9514ca 5 //vcc,sce,rst,dc,mosi,clk,led
MsTee 0:dc77cc9514ca 6 N5110 lcd(p5,p6,p7,p8,p11,p13,p21); //pwm for led backlight
MsTee 0:dc77cc9514ca 7 DigitalIn myswitch(p19); //creates function for the digitalIn switch which enables the switch to display glider or pixels (connects to pin 19 on mbed)
MsTee 0:dc77cc9514ca 8 AnalogIn var(p17); // adds setting for using a potentiometer to adjust brightness of screen (connects to pin 16 on mbed)
MsTee 0:dc77cc9514ca 9 // var = object name
MsTee 0:dc77cc9514ca 10 // note reset button should be pressed always when switch is flipped
MsTee 0:dc77cc9514ca 11
MsTee 0:dc77cc9514ca 12 // function declaration
MsTee 0:dc77cc9514ca 13 int cells[84][48]; // dimen
MsTee 0:dc77cc9514ca 14 int n=0;
MsTee 0:dc77cc9514ca 15 void namedisplay(); //calls function for name display on welcome screen
MsTee 0:dc77cc9514ca 16 void clearCells(); //clears all cells on screen
MsTee 0:dc77cc9514ca 17 void checkerBoard(); // checks that all the pixels are present on the screen
MsTee 0:dc77cc9514ca 18 void checkcells(); //it checks and counts the neighbours and applies the rules
MsTee 0:dc77cc9514ca 19 void updatecells(); // it updates the display for every generation
MsTee 0:dc77cc9514ca 20 void menuDisplay();
MsTee 0:dc77cc9514ca 21
MsTee 0:dc77cc9514ca 22
MsTee 0:dc77cc9514ca 23
MsTee 0:dc77cc9514ca 24 void namedisplay()
MsTee 0:dc77cc9514ca 25 {
MsTee 0:dc77cc9514ca 26 lcd.init();
MsTee 0:dc77cc9514ca 27 lcd.setBrightness(var); // sets the brightness for screen
MsTee 0:dc77cc9514ca 28 lcd.printString("Welcome to" ,15,1); //inserts text for welcome on the first line hence the '1'
MsTee 0:dc77cc9514ca 29 lcd.printString("Conway's",20,2); // prints the text in the brackets on the secondline hence '2'
MsTee 0:dc77cc9514ca 30 lcd.printString("Game of Life",6,3); //prints text in the third line
MsTee 0:dc77cc9514ca 31 lcd.printString(" Thokozile",0,5); // prints name in fifth line
MsTee 0:dc77cc9514ca 32 wait(2); // delays for 2 seconds
MsTee 0:dc77cc9514ca 33 lcd.refresh(); // refreshes screen displays checkerboard (all pixels on screen)
MsTee 0:dc77cc9514ca 34 clearCells(); // clears screen removes cells
MsTee 0:dc77cc9514ca 35 lcd.refresh(); // resets the screen
MsTee 0:dc77cc9514ca 36 }
MsTee 0:dc77cc9514ca 37 void clearCells()
MsTee 0:dc77cc9514ca 38 {
MsTee 0:dc77cc9514ca 39 //loop through cells and clear
MsTee 0:dc77cc9514ca 40 for (int i=0; i<84; i++) { // clears all the cells accross the screen horizontally
MsTee 0:dc77cc9514ca 41 for (int j=0; j<48; j++) { // clears all cells down the screen vertically
MsTee 0:dc77cc9514ca 42 lcd.clearPixel(i,j);
MsTee 0:dc77cc9514ca 43 }
MsTee 0:dc77cc9514ca 44 }
MsTee 0:dc77cc9514ca 45 lcd.refresh();
MsTee 0:dc77cc9514ca 46 }
MsTee 0:dc77cc9514ca 47 void glider()
MsTee 0:dc77cc9514ca 48 {
MsTee 0:dc77cc9514ca 49 lcd.setPixel(20,20);
MsTee 0:dc77cc9514ca 50 lcd.setPixel(21,20);
MsTee 0:dc77cc9514ca 51 lcd.setPixel(22,20);
MsTee 0:dc77cc9514ca 52 lcd.setPixel(22,19);
MsTee 0:dc77cc9514ca 53 lcd.setPixel(21,18);
MsTee 0:dc77cc9514ca 54
MsTee 0:dc77cc9514ca 55 //sets pixels to allow glider to move down and up
MsTee 0:dc77cc9514ca 56 }
MsTee 0:dc77cc9514ca 57 void checkerBoard()
MsTee 0:dc77cc9514ca 58 {
MsTee 0:dc77cc9514ca 59 //loop through every other cell and turn pixel on
MsTee 0:dc77cc9514ca 60 for (int i=0; i<84; i+=2) { //loops through cells horizontally
MsTee 0:dc77cc9514ca 61 for (int j=0; j<48; j+=2) { //loops through cells vertically
MsTee 0:dc77cc9514ca 62 lcd.setPixel(i,j); //sets the pixels accordingly
MsTee 0:dc77cc9514ca 63 }
MsTee 0:dc77cc9514ca 64 }
MsTee 0:dc77cc9514ca 65 lcd.refresh(); //must refresh to write buffer to display
MsTee 0:dc77cc9514ca 66 }
MsTee 0:dc77cc9514ca 67
MsTee 0:dc77cc9514ca 68 void checkcells()
MsTee 0:dc77cc9514ca 69 {
MsTee 0:dc77cc9514ca 70 for (int i=0; i<84; i++) { // checks for neighbours in x-direction
MsTee 0:dc77cc9514ca 71 for (int j=0; j<48; j++) { // checks for neighbours in y-direction
MsTee 0:dc77cc9514ca 72
MsTee 0:dc77cc9514ca 73 int n=0; //number of neighbours
MsTee 0:dc77cc9514ca 74
MsTee 0:dc77cc9514ca 75 if(lcd.getPixel(i-1,j-1)) //pixel to top-left
MsTee 0:dc77cc9514ca 76 n++;
MsTee 0:dc77cc9514ca 77 if(lcd.getPixel(i-1,j)) //pixel to the left
MsTee 0:dc77cc9514ca 78 n++;
MsTee 0:dc77cc9514ca 79 if(lcd.getPixel(i-1,j+1)) //pixel to bottom left
MsTee 0:dc77cc9514ca 80 n++;
MsTee 0:dc77cc9514ca 81 if(lcd.getPixel(i,j-1)) // pixel to top
MsTee 0:dc77cc9514ca 82 n++;
MsTee 0:dc77cc9514ca 83 if(lcd.getPixel(i,j+1)) // pixel to bottom
MsTee 0:dc77cc9514ca 84 n++;
MsTee 0:dc77cc9514ca 85 if(lcd.getPixel(i+1,j-1)) // pixel to top-right
MsTee 0:dc77cc9514ca 86 n++;
MsTee 0:dc77cc9514ca 87 if(lcd.getPixel(i+1,j)) // pixel to right
MsTee 0:dc77cc9514ca 88 n++;
MsTee 0:dc77cc9514ca 89 if(lcd.getPixel(i+1,j+1)) // pixel to bottom-right
MsTee 0:dc77cc9514ca 90 n++;
MsTee 0:dc77cc9514ca 91
MsTee 0:dc77cc9514ca 92 if (lcd.getPixel(i,j)) { // if cells are alive....
MsTee 0:dc77cc9514ca 93 if (n==3 || n==2) // if n is 3 or 2
MsTee 0:dc77cc9514ca 94 cells[i][j]=1; // switch cell on
MsTee 0:dc77cc9514ca 95 else if (n<2 || n>3) // if n is less than 2 or 3
MsTee 0:dc77cc9514ca 96 cells[i][j]=0; // switch cell off
MsTee 0:dc77cc9514ca 97 } else { // else if dead...
MsTee 0:dc77cc9514ca 98 if (n==3)
MsTee 0:dc77cc9514ca 99 cells[i][j]=1; // keep cell on
MsTee 0:dc77cc9514ca 100 }
MsTee 0:dc77cc9514ca 101 }
MsTee 0:dc77cc9514ca 102 }
MsTee 0:dc77cc9514ca 103 }
MsTee 0:dc77cc9514ca 104
MsTee 0:dc77cc9514ca 105 void updatecells() // updates display for after checking neighbours
MsTee 0:dc77cc9514ca 106 {
MsTee 0:dc77cc9514ca 107 lcd.setBrightness(var); // sets function to adjust brightness
MsTee 0:dc77cc9514ca 108
MsTee 0:dc77cc9514ca 109 for(int i=0; i<84; i++) { // updates for cells in x-direction
MsTee 0:dc77cc9514ca 110 for(int j=0; j<48; j++) { // updates for y-direction
MsTee 0:dc77cc9514ca 111
MsTee 0:dc77cc9514ca 112 if(cells[i][j]==1) { //if cells are on
MsTee 0:dc77cc9514ca 113 lcd.setPixel(i,j); //set the pixels accordingly
MsTee 0:dc77cc9514ca 114 } else {
MsTee 0:dc77cc9514ca 115 lcd.clearPixel(i,j); // clear pixels when off
MsTee 0:dc77cc9514ca 116 }
MsTee 0:dc77cc9514ca 117 }
MsTee 0:dc77cc9514ca 118 }
MsTee 0:dc77cc9514ca 119 lcd.refresh(); //reset
MsTee 0:dc77cc9514ca 120 }
MsTee 0:dc77cc9514ca 121
MsTee 0:dc77cc9514ca 122
MsTee 0:dc77cc9514ca 123
MsTee 0:dc77cc9514ca 124 int main() // main function to call all functions
MsTee 0:dc77cc9514ca 125 {
MsTee 0:dc77cc9514ca 126 lcd.init();
MsTee 0:dc77cc9514ca 127 namedisplay();
MsTee 0:dc77cc9514ca 128 checkerBoard();
MsTee 0:dc77cc9514ca 129 wait(1.5);
MsTee 0:dc77cc9514ca 130 clearCells();
MsTee 0:dc77cc9514ca 131 wait(1.5);
MsTee 0:dc77cc9514ca 132 if (myswitch==1) { // function for switch on
MsTee 0:dc77cc9514ca 133 lcd.randomiseBuffer(); //randomise cells if swtich on
MsTee 0:dc77cc9514ca 134 } else if (myswitch==0) { // display glider on 2 and pixels on switch 1
MsTee 0:dc77cc9514ca 135
MsTee 0:dc77cc9514ca 136 glider();
MsTee 0:dc77cc9514ca 137 }
MsTee 0:dc77cc9514ca 138 while(1) { // while loop
MsTee 0:dc77cc9514ca 139 checkcells();
MsTee 0:dc77cc9514ca 140 updatecells();
MsTee 0:dc77cc9514ca 141 wait(0.3); // the rate at which cells degenerate
MsTee 0:dc77cc9514ca 142 lcd.refresh();
MsTee 0:dc77cc9514ca 143 }
MsTee 0:dc77cc9514ca 144 }