1- // STROOP TEST
21$ ( function ( ) {
32 const colors = [ 'red' , 'green' , 'blue' , 'orange' , 'purple' , 'pink' ] ;
43 let timer , score , interval ;
54
65 function initializeGame ( ) {
76 timer = 30 ;
87 score = 0 ;
9- $ ( '#start-stroop-test' ) . show ( ) ;
10- $ ( '#controls, #stroop-word ' ) . addClass ( 'hidden ' ) ;
11- $ ( '#stroop-timer' ) . text ( `Time remaining : ${ timer } seconds ` ) ;
8+
9+ $ ( '#progress-bar' ) . css ( 'width' , '100%' ) . removeClass ( 'bg-yellow-500 bg-red-500 ') . addClass ( 'bg-blue-600 ' ) ;
10+ $ ( '#stroop-timer' ) . text ( `Time: ${ timer } s ` ) ;
1211 $ ( '#score' ) . text ( `Score: ${ score } ` ) ;
12+
13+ $ ( '.start-button-container' ) . show ( ) ;
14+ $ ( '#game-area, #results' ) . addClass ( 'hidden' ) ;
15+
16+ $ ( '#color-buttons' ) . empty ( ) ;
17+ colors . forEach ( color => {
18+ $ ( '#color-buttons' ) . append (
19+ `<button class="bg-${ color } -500 hover:bg-${ color } -600 active:bg-${ color } -700 text-white rounded-md px-6 py-3 font-medium text-lg transition-colors"
20+ data-color="${ color } ">${ color } </button>`
21+ ) ;
22+ } ) ;
1323 }
1424
1525 function shuffleArray ( array ) {
16- for ( let i = array . length - 1 ; i > 0 ; i -- ) {
26+ const newArray = [ ...array ] ;
27+ for ( let i = newArray . length - 1 ; i > 0 ; i -- ) {
1728 const j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ;
18- [ array [ i ] , array [ j ] ] = [ array [ j ] , array [ i ] ] ;
29+ [ newArray [ i ] , newArray [ j ] ] = [ newArray [ j ] , newArray [ i ] ] ;
1930 }
31+ return newArray ;
2032 }
2133
2234 function gameOver ( ) {
23- alert ( `Game Over! Your final score is ${ score } ` ) ;
2435 clearInterval ( interval ) ;
25- saveScore ( "Stroop Test" , score ) ;
26- initializeGame ( ) ;
27- }
2836
37+ $ ( '#final-score' ) . text ( `Score: ${ score } ` ) ;
2938
30- let displayColor = '' ;
39+ saveScore ( "Stroop Test" , score ) ;
40+
41+ $ ( '#game-area' ) . addClass ( 'hidden' ) ;
42+ $ ( '#results' ) . removeClass ( 'hidden' ) ;
43+ }
3144
3245 function generateStroop ( ) {
33- const actualColor = colors [ Math . floor ( Math . random ( ) * colors . length ) ] ;
34- const oldDisplayColor = displayColor ;
35- displayColor = colors [ Math . floor ( Math . random ( ) * colors . length ) ] ;
46+ const colorName = colors [ Math . floor ( Math . random ( ) * colors . length ) ] ;
47+ const textColor = colors [ Math . floor ( Math . random ( ) * colors . length ) ] ;
3648
3749 const $stroop = $ ( '#stroop-word' ) ;
38- $stroop . removeClass ( `text-${ oldDisplayColor } -500` ) . addClass ( `text-${ displayColor } -500` ) ;
39- $stroop . text ( actualColor ) . data ( 'color' , displayColor ) ;
4050
41- shuffledColors = [ ...colors ] ;
42- shuffleArray ( shuffledColors ) ;
43-
44- $ ( '#color-buttons' ) . children ( ) . each ( function ( index ) {
45- const oldColor = $ ( this ) . data ( 'bg-color' ) ; // Retrieve old color from data attribute
46-
47- if ( oldColor ) { // Remove classes only if oldColor exists
48- $ ( this ) . removeClass ( `bg-${ oldColor } -500 hover:bg-${ oldColor } -700 active:bg-${ oldColor } -500` ) ;
49- }
50-
51- $ ( this ) . addClass ( `bg-${ shuffledColors [ index ] } -500 hover:bg-${ shuffledColors [ index ] } -700 active:bg-${ shuffledColors [ index ] } -500` )
52- . text ( colors [ index ] )
53- . data ( 'bg-color' , shuffledColors [ index ] ) ; // Store new color in data attribute
51+ colors . forEach ( color => {
52+ $stroop . removeClass ( `text-${ color } -500` ) ;
5453 } ) ;
54+
55+ $stroop . addClass ( `text-${ textColor } -500` ) ;
56+ $stroop . text ( colorName . toUpperCase ( ) ) . data ( 'color' , textColor ) ;
5557 }
5658
57- // Initialize game state
5859 initializeGame ( ) ;
5960
60- // Initial randomized button creation
61- let shuffledColors = [ ...colors ] ;
62- shuffleArray ( shuffledColors ) ;
63-
64- shuffledColors . forEach ( ( color , index ) => {
65- $ ( '#color-buttons' ) . append ( `<button class="bg-${ color } -500 hover:bg-${ color } -700 active:bg-${ color } -500 text-white rounded px-8 py-4 ml-1">${ colors [ index ] } </button>` ) ;
66- } ) ;
61+ $ ( '#start-stroop-test' ) . click ( function ( ) {
62+ $ ( '.start-button-container' ) . hide ( ) ;
63+ $ ( '#game-area' ) . removeClass ( 'hidden' ) ;
6764
68- $ ( '#start-stroop-test' ) . click ( function ( ) {
69- $ ( this ) . hide ( ) ;
70- $ ( '#controls, #stroop-word' ) . removeClass ( 'hidden' ) ;
65+ generateStroop ( ) ;
7166
72- interval = setInterval ( function ( ) {
67+ interval = setInterval ( function ( ) {
7368 if ( timer > 0 ) {
7469 timer -- ;
75- $ ( '#stroop-timer' ) . text ( `Time remaining: ${ timer } seconds` ) ;
70+ const percentage = ( timer / 30 ) * 100 ;
71+ $ ( '#progress-bar' ) . css ( 'width' , `${ percentage } %` ) ;
72+
73+ if ( timer <= 10 ) {
74+ $ ( '#progress-bar' ) . removeClass ( 'bg-blue-600 bg-yellow-500' ) . addClass ( 'bg-red-500' ) ;
75+ } else if ( timer <= 20 ) {
76+ $ ( '#progress-bar' ) . removeClass ( 'bg-blue-600 bg-red-500' ) . addClass ( 'bg-yellow-500' ) ;
77+ }
78+
79+ $ ( '#stroop-timer' ) . text ( `Time: ${ timer } s` ) ;
7680 } else {
7781 gameOver ( ) ;
7882 }
7983 } , 1000 ) ;
84+ } ) ;
8085
81- generateStroop ( ) ;
86+ $ ( '#play-again' ) . click ( function ( ) {
87+ initializeGame ( ) ;
8288 } ) ;
8389
84- $ ( '#color-buttons' ) . on ( 'click' , 'button' , function ( ) {
85- const chosenColor = $ ( this ) . text ( ) ;
86- if ( chosenColor === $ ( '#stroop-word' ) . data ( 'color' ) ) {
90+ $ ( '#color-buttons' ) . on ( 'click' , 'button' , function ( ) {
91+ const chosenColor = $ ( this ) . data ( 'color' ) ;
92+ const correctColor = $ ( '#stroop-word' ) . data ( 'color' ) ;
93+
94+ if ( chosenColor === correctColor ) {
8795 score ++ ;
8896 $ ( '#score' ) . text ( `Score: ${ score } ` ) ;
97+ generateStroop ( ) ;
8998 } else {
9099 gameOver ( ) ;
91100 }
92- generateStroop ( ) ;
93101 } ) ;
94102} ) ;
0 commit comments