/**
 * 		Logiku 2014 - version 1   
 *
 * 		Open source public domain software - Alexor (2014) & Andrea Bjelogrlic (2015)
 *
 */

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;


public class Logiku extends JFrame{
	private JButton p;
	private JButton q;
	private JButton s;
	private JButton r;
	private static JButton help;
	private static  boolean[] log_var=new boolean[14];		// logical random variables
	private static boolean[] log_result=new boolean[7];		// line results
	private static boolean var_p=false;
	private static boolean var_q=false;
	private static boolean var_s=false;
	private static boolean var_r=false;
	private static JLabel[] line=new JLabel[7]; //label containing logical_string
	private static String[] logical_string= new String[7];
	private static int turn=0;	//turn counter
	private static int [] score=new int[6];
	private static int totscore=0;
	private static JLabel scorelabel;
	private static boolean play=true;
	private static JButton yes;
	private static JButton no;
	private static JLabel playagain;
	
	
	
		Logiku(){
			
		setLayout(null);
			
		
		playagain=new JLabel();
		playagain.setBounds(250,300,200,50);
		playagain.setVisible(false);
		playagain.setText("do you want to play again?");
		add(playagain);
		
		yes=new JButton("yes");
		yes.setBounds(250,350,100,50);
		yes.setVisible(false);
		add(yes);
		
		
		no=new JButton("no");
		no.setBounds(350,350,100,50);
		no.setVisible(false);
		add(no);
		
		
		scorelabel=new JLabel();
		scorelabel.setBounds(0,500,300,100);
		scorelabel.setVisible(true);
		scorelabel.setFont(new Font("Arial", Font.PLAIN, 20));

		add(scorelabel);
			
		help=new JButton("help");
		help.setBounds(0,600,100,50);
		help.setVisible(true);
		add(help);
			
		p=new JButton("P");
		p.setBounds(200,600,50,50);
		p.setVisible(true);
		add(p);
		
		q=new JButton("Q");
		q.setBounds(300,600,50,50);
		q.setVisible(true);
		add(q);
		
		s=new JButton("S");
		s.setBounds(400,600,50,50);
		s.setVisible(true);
		add(s);
		
		
		r=new JButton("R");
		r.setBounds(500,600,50,50);
		r.setVisible(true);
		add(r);
		
		
		
		
		
		int y=10;
		for (int i=0;i<7;i++){
			line[i]=new JLabel();
			line[i].setFont(new Font("Arial", Font.PLAIN, 20));
			line[i].setBounds(10,y,690,20);
			y=y+60;
			add(line[i]);
		}
			
		
		eventhelp helpe=new eventhelp();			// add action listener to button help
		help.addActionListener(helpe);
		
		
		eventsetp true_p=new eventsetp();		// add action listener to button p
		p.addActionListener(true_p);
		
		
		eventsets true_s=new eventsets();		//add action listener to button s
		s.addActionListener(true_s);
		
		eventsetq true_q=new eventsetq();		// add action listener to button q
		q.addActionListener(true_q);
		
		eventsetr true_r=new eventsetr();		//add action listener to button r
		r.addActionListener(true_r);
		
		
		
		eventno nope=new eventno();
		no.addActionListener(nope);
		
		eventyep yep=new eventyep();
		yes.addActionListener(yep);
		
		
		
		}
		
		
		// exit game
		
		public class eventno implements ActionListener{

			
			public void actionPerformed(ActionEvent nope) {
				System.exit(0);
				
			}
			
		}
		
		
		//play again
		
		public class eventyep implements ActionListener{

			
			public void actionPerformed(ActionEvent yep) {
				turn=0;
				for(int i=0;i<14;i++){
					double ran;
					ran=Math.random();
					if(ran<0.5)
						log_var[i]=false;
					
					else
						log_var[i]=true;
						
				}
				
				
				for(int i=0;i<7;i++){
					line[i].setVisible(true);
				}
				var_p=false;
				var_r=false;
				var_q=false;
				var_s=false;
				
				s.setVisible(true);
				r.setVisible(true);
				q.setVisible(true);
				p.setVisible(true);
				
				for(int i=0;i<5;i++){
					score[i]=0;
				}
				totscore=0;
				
					help.setVisible(true);
					scorelabel.setVisible(true);
					play=true;
					yes.setVisible(false);
					no.setVisible(false);
					playagain.setVisible(false);
				
				
			}
		
		}
		
		
		// checks if game is over
		
		public static boolean gameover(){
			
		if(var_p && var_q && var_s && var_r && score[4] !=0){	
			return true; 
		}else
			return false;
		
		}
		
		
	
		
		
		
		
		/*
		 * change boolean variable r to true
		 */
		
		
		public class eventsetr implements ActionListener{
			public void actionPerformed(ActionEvent true_r) {
				turn++;
				var_r=true;
				r.setVisible(false);
				
				
			}
			
		}
		
		
		
		
		/*
		 * change boolean variable q to true
		 */
		
		public class eventsetq implements ActionListener{
			public void actionPerformed(ActionEvent true_q) {
				turn++;
				var_q=true;
				q.setVisible(false);
				
				
			}
			
		}
		
		
		
		/*
		 * change boolean variable s to true
		 */
		
		
		public class eventsets implements ActionListener{
			public void actionPerformed(ActionEvent true_s) {
				 turn++;
				var_s=true;
				s.setVisible(false);
			   
			}
			
		}
		
		
		
		
		
		
		
		
		
		/*
		 * change boolean variable p to true
		 */
		
		public class eventsetp implements ActionListener{
			public void actionPerformed(ActionEvent true_p) {
				 turn++;
				var_p=true;
				p.setVisible(false);
				
			}
			
		}
	
		
		
		
		
		
		/*
		 * event help class
		 * creates popup window with instructions
		 */
		
	public class eventhelp implements ActionListener{

		
		public void actionPerformed(ActionEvent helpe) {
	JOptionPane.showMessageDialog(null,"At the beginning, P, Q, R and S are FALSE. At the end of the game, after 4 steps, they are TRUE.","Help",JOptionPane.PLAIN_MESSAGE);
	JOptionPane.showMessageDialog(null,"Essentially, you have only to choose in which order to change each of them from FALSE to TRUE, in order to sum the highest score", "Help", JOptionPane.PLAIN_MESSAGE);

			
		}
		
	}
	
	
	
	
	
	
	
	
	
	
	public static void main(String[]args){
		
		
		
		JOptionPane.showMessageDialog(null,"Open source public domain software - Alexor (2014) & Andrea Bjelogrlic (2015)", "Logiku 2014 - version 1  ", JOptionPane.PLAIN_MESSAGE);	

		
		//creates frame
		
		
		Logiku frame=new Logiku();
		frame.setSize(700,670);
		frame.setVisible(true);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("Logiku");
		
		
		
		for(int i=0;i<5;i++){
			score[i]=0;
		}
		
		
		
		//  initialize boolean variables
		 
		for(int i=0;i<14;i++){
			double ran;
			ran=Math.random();
			if(ran<0.5)
				log_var[i]=false;
			
			else
				log_var[i]=true;
				
		}
		
		
		
		while(true){
			
			
			
			while(play){  
				
				
				
			//check result of expression
			
			
			
			
			
			log_result[0]=((var_p && log_var[0]) || (log_var[1] && var_s));
			log_result[1]=(log_var[2] && (var_q || (var_r && log_var[3])));
			log_result[2]=(((var_p && log_var[4]) || log_var[5]) && var_s);
			log_result[3]=((var_s && !log_var[6]) || (var_r && !log_var[7]));
			log_result[4]=(var_q && ! log_var[8]) || (var_p && !log_var[9]); 
			log_result[5]=((var_s || ! log_var[10]) && !(var_r || log_var[11]));
			log_result[6]=((var_p || ! log_var[12]) && !(var_s || log_var[13]));


			
			
			
			logical_string[0]="( P AND " +log_var[0] + ") OR ( " + log_var[1] +" AND S ) = " +log_result[0]  ;
			logical_string[1]="" + log_var[2] + " AND [ Q OR ( R AND " +log_var[3] +" ) ]= " +log_result[1];
			logical_string[2]="[ ( P AND " + log_var[4] + " ) OR "+log_var[5]+ " ] AND S = " +log_result[2] ;
			logical_string[3]="( S AND NOT " +log_var[6]+ " ) OR ( R AND NOT " +log_var[7]+ " )=" +log_result[3];
			logical_string[4]="( Q AND NOT " +log_var[8]+" ) OR ( P AND NOT " +log_var[9]+ " ) " +log_result[4];
			logical_string[5]="( S OR NOT " +log_var[10]+ " ) AND NOT ( R OR "+log_var[11]+ " )=" +log_result[5];
			logical_string[6]="( P OR NOT " +log_var[12]+ " ) AND NOT ( S OR "+log_var[13]+ " ) =" +log_result[6];
			
			
			 
			//change expressions string
			//set logical string to label
			
			for(int i=0;i<7;i++){
			line[i].setText(logical_string[i]);
			}
			
			
			int temp=0;
			for (int i=0;i<7;i++){
				
				if(log_result[i]==true){
					temp++;
				}
				score[turn]=temp;
			}
			
			
			
			
			
			totscore=score[0]+score[1]+score[2]+score[3]+score[4];
			
			
			
			
			
			
			
			scorelabel.setText("total score: " +totscore+ "          score: " +score[turn]  );
			
			
			
			
			if(gameover()){
				JOptionPane.showMessageDialog(null, "you scored: "+totscore+" points","game over",JOptionPane.PLAIN_MESSAGE);
				for(int i=0;i<7;i++){
					line[i].setVisible(false);
				}
					help.setVisible(false);
					scorelabel.setVisible(false);
					play=false;
					yes.setVisible(true);
					no.setVisible(true);
					playagain.setVisible(true);
					
					
					
					
				
			}
			
			
			
			
			}
			
			
}
		
		
		
		
		
		
		
	}
	

}